Skip to content

Pytest

Kevin Kuns requested to merge kevin.kuns/finesse-ligo:pytest into ddb

This does a few things to improve the unit tests. Some of the "test" files didn't actually have any unit tests. So this converts some of those into quick unit tests. In part to facilitate this, but also for more general convenience, gwinc and wield like pytest fixtures are added to conftest.py. These make it easier to treat unit tests similar to notebook or vscode cells.

The main point of this is to allow example code to run in a test even if there hasn't been time to write a unit test that asserts some condition. This is useful because a) this test can be testing a piece of code not covered by other tests and the failure to even run (whether the answer is correct or not) immediately indicates a bug and b) it's just useful to see test outputs in real time as an issue can be obvious just by looking at a plot for example. So some of that example code was made into these kinds of unit tests

So that these more example and exploratory tests don't need to be run everytime if people just want to run the tests asserting some condition, a pytest marker "example" is added to mark these example tests. They will only be run by giving the --examples option to pytest. So for example, you can write a test that does some calculations and saves plots like this

@pytest.mark.example
def calculate_stuff(tpath_join):
    model = finesse.Model()
    # do stuff
    # make some figure fig
    fig.savefig(tpath_join("figure.pdf"))

This will generate the (local) plot "test_results/calculate_stuff/figure.pdf" which will be continuously updated by running

ptw -- tests/test_file.py --tb short -s -k calculate_stuff --examples
Edited by Kevin Kuns

Merge request reports

Loading