Skip to main content

Framework for writing tests for Firefly Zero apps

Project description

firefly-test

Framework for writing tests for Firefly Zero apps. This is a Python library, so the tests using it are writtten in Pytohn as well. However, you can test a Firefly Zero app or game written in any language, not just Python.

If you don't know Python, don't panic: it's a simple language for simple tasks, and you can learn it good enoug to write your first tests in a matter of minutes. And this documentation will help you.

Installation

python3 -m pip install firefly-test

Installation from the source is currently not possible: the project depends on firefly-runtime which is not open-sourced yet (but will be soon). The wheel distributions on PyPI contain the compiled binaries for the runtime.

Writing tests in Python

The most popular and simple tool for running Python tests is pytest. Make sure you have it installed:

python3 -m pip install --break-system-packages pytest

Tests should be placed in the tests directory in the root of the project. Each file with tests should start with test_. And each test function also should start with test_. For example, create tests/test_math.py:

import math

def test_cos():
    assert math.cos(0.0) == 1.0

Now, run the tests:

pytest

You can read more in the pytest documentation: docs.pytest.org.

Installing your app

The framework tests not your source code but a compiled and installed app. So, make sure to build and install your project:

firefly_cli build

In the examples below, we'll be using sys.input-test as our test target. If you want to follow along, make sure you have it installed:

firefly_cli import sys.input-test

Using firefly-test

The App class accepts the ID of the app you want to test and provides methods and attributes for interacting with the app:

from firefly_test import App
app = App('sys.input-test')

The first thing you should do is start your app. It will initialize the app memory, call the boot callback, etc.

app.start()

Now each time you call update, it will run one update cycle: call the update and render app callbacks, read inputs, flash the image from the frame buffer on the fake screen, etc.

app.update()

After the update, you can access the frame buffer using the frame attribute. The frame has a lot of helpful methods for checking the image it contains. For example, you can use the at method to get the color value of the pixel at the given coordinates:

from firefly_test import Color
assert app.frame.at(x=0, y=0) == Color.WHITE

we can iterate over all pixels and, for example, check that every pixel is one of the 3 expected colors:

allowed_colors = {Color.WHITE, Color.LIGHT_GRAY, Color.GRAY}
for color in app.frame:
    assert color in allowed_colors

The update method may also accept Input. This is the input value that this and all subsequent colors will receive (until overwritten). For example, check that pressing the S button changes the color fo the pixel (x=185, y=100) from white to light blue:

assert app.frame.at(185, 100) == Color.WHITE
app.update(Input(s=True))
assert app.frame.at(185, 100) == Color.LIGHT_BLUE

You can find this test (and the others covered below) in the tests/test_integration.py file.

Pattern testing

You can assert that a subregion of a frame matches a pattern. A pattern is an ASCII where each symbols represent an expected color:

  • .: any color.
  • K: black.
  • P: purple.
  • R: red.
  • O: orange.
  • Y: yellow.
  • G: green.
  • g: light green.
  • D: dark green.
  • B: blue.
  • d: dark blue.
  • b: light blue.
  • C: cyan.
  • W: white.
  • : light gray.
  • : gray.
  • : dark gray.

Take a frame subregion:

circle = app.frame.get_sub(
    x=160, y=100, width=20, height=5,
)

Assert that it matches a pattern:

circle.assert_match("""
    WWWW◑◑◑◑◑◑◑◑◑◑◑◑WWWW
    WWW◑◑◑◑WWWWWW◑◑◑◑WWW
    WW◑◑◑WWWWWWWWWW◑◑◑WW
    W◑◑◑WWWWWWWWWWWW◑◑◑W
    ◑◑◑WWWWWWWWWWWWWW◑◑◑
""")

In this example, we checked that the selected region is a gray circle's top on a white background.

Snapshot testing

You can compare a frame or frame region to a snapshot:

from pathlib import Path
snapshots = Path(__file__).parent / '.snapshots'
app.update()
app.frame.assert_match(snapshots / 'default')

On the first run, the test will save the frame in the .snapshots/default file. When you run it the next time, it will read the old frame from the file and compare it to the current one. If they mismatch, even by one pixel, the test will fail. You can use to_png method of the frame to save it into a PNG file and see how it looks like. If the change is desirable, you can remove the old snapshot and the next run will save the new snapshot.

License

MIT License. You can freely use it for testing any apps and games, free or commercial, open-source or proprietary. Happy hacking!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

firefly_test-0.1.5.tar.gz (40.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

firefly_test-0.1.5-cp314-cp314-manylinux_2_38_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.38+ x86-64

firefly_test-0.1.5-cp314-cp314-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

firefly_test-0.1.5-cp314-cp314-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

firefly_test-0.1.5-cp313-cp313-manylinux_2_38_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.38+ x86-64

firefly_test-0.1.5-cp313-cp313-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

firefly_test-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

firefly_test-0.1.5-cp312-cp312-manylinux_2_38_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.38+ x86-64

firefly_test-0.1.5-cp312-cp312-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

firefly_test-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

Details for the file firefly_test-0.1.5.tar.gz.

File metadata

  • Download URL: firefly_test-0.1.5.tar.gz
  • Upload date:
  • Size: 40.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for firefly_test-0.1.5.tar.gz
Algorithm Hash digest
SHA256 d64a3d32bda4d3e8c9e4bf70fa9d68078898eaa68f14bf35cee463bbcae2af50
MD5 23a2b5f5d0fbc4befc1b8f4cf1fce210
BLAKE2b-256 666cfb3b41e8d06e9f30ee4c76e72595f274f274b6801e1a14069afda5e2fc29

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp314-cp314-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp314-cp314-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 52ceb206cad72ab9731db3cfce28a7ec27e51b82c942ba7525a0c7b38d8f6987
MD5 8ead0fad60e6f9036386447a26d7cf8d
BLAKE2b-256 b967747c835e31fc03a54a8dfa4a941cbbf5bd76d38403c8595586afd7494474

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bae8bc2963f850f4e392f638b559c7549cec803bf6597fd66b0e631b91a8f648
MD5 8474b6dc6bb1071ac17ce6702dd7fb1a
BLAKE2b-256 fcbbc3c9ac45dee8b3bef76b732de3ef27729ed2f2e2871014e9269ad24bb88d

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e0798352534ed18a330a60f59174b72fc1aeb2a42b722e7290e68a5093ec9d80
MD5 5b527d67fd3f643a429234db3ee73aa0
BLAKE2b-256 25d27c78aada736ea524c84d20df22fdab7e83b880226317e4cddb4b20391f77

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp313-cp313-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp313-cp313-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 97e08814becda06c965c69adab0555153320cd695ef83c9e5227ac0230a13091
MD5 d154a789fd76dbc7fad2172b18cfec65
BLAKE2b-256 4c8cb4c51ed6df10276fd64dc1be0fb41bd414f70ea702068031e91e12bc8205

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d2a2148469e14f8d93eb9eed0ff0fef33db860ba490b897e72d05f0686841278
MD5 5dfbaacd43b271d7dc14033f8781772f
BLAKE2b-256 597352d14630dfea62f82122cc7c73d6282357f86324d0bdf888562a221afc6b

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8f378d58188718c6a003cfe523434b07397fb1569062a91b284ff41f647cb6d3
MD5 b6b22fde6ae80e83fba1d6451d956618
BLAKE2b-256 8aab436ba063508cfdb91ca11eb7ccc479f63ee2f1dafd170f4908d810c36f80

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp312-cp312-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp312-cp312-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 7385006d3d2e763148a3c329c487e62e9c4ae27e0610200d7e87aaf9ea0bd0d0
MD5 206c2a717d708770163cba41df3abf4f
BLAKE2b-256 d544db1c6f516a2ccf7a8979de9c246d3d2993071644f638244282632a4ffcf5

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 35ad61d6a6b76d3210ad4ab0a534d8e9ec6122d4579aba074a926d6d5a1bdf39
MD5 65632102bdbcd9dba252dab61462a344
BLAKE2b-256 9f48182cc623aa87a758c05fedbc45f453815c5b8c8ae79a7957be3e601045f2

See more details on using hashes here.

File details

Details for the file firefly_test-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for firefly_test-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 685ef2292bf2ebe7f60e8dfa06c7f1ac926be22db44c8f309a96121f5b4df572
MD5 7ee00f51d4aa72e9c9f08c19d6e3795b
BLAKE2b-256 7f5c6edbd45eaa73ecd064c0c949d7b864dcca6a39f4e1f22c7408f0b144467c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page