Testing

Because sgl strives to expose all of its API to Python, most unit tests should be written in Python. However, some C++ unit tests are still needed for parts that are not exposed to Python.

Python Unit Tests

Python unit tests are written in the pytest framework.

Unit tests are located next to where the code they test is located. For example, the code for the Bitmap class is located in src/sgl/core/bitmap.cpp, and it’s corresponding unit test is located in src/sgl/core/tests/test_bitmap.py.

In order to run unit tests, you need to have the pytest package installed in your active Python environment. You also need to have the sgl package available. If you run a development build, use the setpath.bat or setpath.sh script to make the package available.

To run the tests, simply run the following command from the root directory:

pytest src

To run all tests in a specific file, you can specify the file to run:

pytest src/sgl/core/tests/test_bitmap.py

To run just one specific test case, you can additionally set the -k flag:

pytest src/sgl/core/tests/test_bitmap.py -k test_jpg_io

Each test source file should also be executable as a standalone script. This is most useful when debugging from an IDE. Allowing scripts to be run standalone is accomplished by adding the following code at the end of each file:

if __name__ == "__main__":
    pytest.main([__file__, "-v"])

C++ Unit Tests

C++ unit tests are written in the doctest framework.

The main application entry point is located in src/sgl/tests/sgl_tests.cpp. Similar to the Python unit tests, the C++ unit tests are located next to the code they test in a tests directory.

To run the tests, simply run the following command from the binary output directory:

./sgl_tests