3. Testing Conventions#

3.1. Test Tools#

We use pytest with coverage reports. Install the following packages for testing.

pip install -U pytest
pip install -U pytest-cov

3.2. Test Scripts Organizing#

Test scripts should be named and placed as suggested in the following way.

molass-library/
    tests/
        generic/
            010_DataObjects/
                test_010_SSD.py
                test_020_Curve.py
                ...
            020_DataUtils/
                test_010_DataUtils.py
                ...
        specific/
            010_Geometric/
                test_010_Peaklike.py
                ...
            020_FlowChange/
                test_010_FlowChange.py

One reason for dividing tests into the two categories, namely “generic” and “specific”, is as follows.

The generic folder should be kept relatively simple by excluding site-specific tests such as “FlowChange”, which arises only from unique experiment practice at some SAXS beamlines in Photon Factory, Japan and often includes complex issues irrelevant to generic cases.

Note

Three-digit numbers are just for ordering in the following sense.

  • ease for glancing over roughly in created (or logical) order

  • order of test execution

3.3. Running Tests#

3.3.1. Test Data Settings#

Depending on the category, used data will vary as shown below.

Table 3.1 Data Usage depending on Test Category#

Data Name / test folder

generic tests usage

specific tests usage

ORIGINAL_DATA

Yes

Yes

TUTORIAL_DATA

Yes

Yes

DATA_ROOT_FOLDER

No

Yes

test folder

tests/generic

tests/specific

The data names above are partly the same as those referenced in Local Settings of the Tutorial, the locations of which you should specify in the local_settings.py script as stated there. It would look something like the following code.

LocalSettings = dict(
    TUTORIAL_DATA=r"D:\MolassData\tutorial_data",
    ORIGINAL_DATA=r"D:\MolassData\original_data",
    DATA_ROOT_FOLDER=r"D:\AllExperimentData",
)

Data sets for DATA_ROOT_FOLDER is currently not provided for download from the book pages. If you need them, let us know by opening an issue using the github icon button at upper right corner of this page.

3.3.2. Command Lines#

To run generic tests only, do as follows in command prompt:

cd tests/generic
pytest --cov=molass

To run specic tests only, do as follows in command prompt:

cd tests/specific
pytest --cov=molass

To run all tests, do as follows in command prompt:

cd tests
pytest --cov=molass

A note on the python path of this pytest execution

The python path in this execution is ensured to be set as the current repositoy root by the following specification in pyproject.toml.

[tool.pytest.ini_options]
pythonpath = [
  "."
]

For detailed tests coverage, execute as follows:

pytest --cov=molass --cov-report=html

and check the results in test_folder/htmlcov/index.html with your browser, where test_folder is one of tests/generic, tests/specific or tests depending on how you ran the tests.

3.3.3. Efficient Testing During Development#

When developing new features, running all tests can be time-consuming. For quick checks, you can simply run a specific test file or function using pytest. For example:

pytest path/to/test_file.py
pytest path/to/test_file.py::test_function_name

More broadly, you can ask GitHub Copilot:

“I use pytest to run all tests, which is great for checking overall software quality. But when developing a new feature, I need quick checks for specific parts of the code. Any advice on how to do this efficiently?”

3.3.4. Attribution#

Some of the insights and suggestions in this chapter were generated with the assistance of GitHub Copilot, an AI programming assistant. Its contributions helped refine the content and provide practical examples for testing workflows.