convexengineering / gpfit

Fit posynomials to data
http://gpfit.readthedocs.io/en/latest/
MIT License
10 stars 7 forks source link

pytest for plot unit tests #110

Closed pgkirsch closed 2 years ago

pgkirsch commented 2 years ago

To partially address #101

pgkirsch commented 2 years ago

@bqpd @galbramc I'm using pytest-mpl to unit test the plotting functions. Does that mean I need to create an equivalent of gpkit.test.helpers.run_tests that can generate xml from pytest unit tests?

galbramc commented 2 years ago

That would be good. Are there other options for generating files with the results of the tests that can be parsed by JenkinsCI?

pgkirsch commented 2 years ago

Looks like yes! https://docs.pytest.org/en/latest/how-to/output.html?highlight=jenkins

To create result files which can be read by Jenkins or other Continuous integration servers, use this invocation:

pytest --junitxml=path

to create an XML file at path.

All of the GPfit unit tests can now be run with pytest by the way pytest tests/. There are 9 unit tests that use pytest-mpl so I believe the complete command will need to be:

pytest --junitxml=path --mpl
galbramc commented 2 years ago

So do we need to change the scripts for how to run the units tests now? Here is how they are currently executed:

echo "import gpfit.tests; gpfit.tests.run(xmloutput=True)" > test.py
echo "Current working directory:"
ls `pwd`
python $COVERAGE run --source=$WORKSPACE/gpfit --omit=$WORKSPACE/gpfit/tests/* test.py
python $COVERAGE xml
python $COVERAGE html -d $WORKSPACE/htmlcov

# Execute the examples to make sure they work (but they don't contribute to coverage)
for i in `ls $WORKSPACE/docs/source/examples/*.py`; do
  python $i
done
pgkirsch commented 2 years ago

Is $COVERAGE an environment variable to do with coverage.py?

If so, I believe that 4th line can be changed to:

python $COVERAGE run --source=$WORKSPACE/gpfit --omit=$WORKSPACE/gpfit/tests/* pytest --junitxml="test_reports" --mpl

The first 3 lines can then be deleted...I think (unless you want to keep the 2nd/3rd for debugging).

galbramc commented 2 years ago

Sorry this fell off my radar. Yes $COVERAGE refers to coverage.py. The 2nd two lines are processing the coverage information after running the tests with the first line. Can you confirm that running things locally on your machine this works:

python `which coverage` run --source=gpfit --omit=gpfit/tests/* test.py pytest --junitxml="test_reports" --mpl

Whats the reason for moving to pytest btw?

pgkirsch commented 2 years ago

No worries!

That command doesn't run but the following seems to work correctly:

 `which coverage` run -m --source=gpfit --omit=gpfit/tests/* pytest --junitxml="test_reports" --mpl

(need -m but don't need python or test.py)

pytest has a neat extension called pytest-mpl that allows for unit testing of plots generated with matplotlib. It also turns out that pytest can run all unit tests written using python's native unittest module so the migration required barely any changes to the testing code.

bqpd commented 2 years ago

oh sounds like i ought to look into that for gpkit as well

galbramc commented 2 years ago

test this please

galbramc commented 2 years ago

test this please

galbramc commented 2 years ago

Does --junitxml=test_reports generate a directory containing xml files, or does it make one single xml file called "test_reports"?

pgkirsch commented 2 years ago

It seems to create one single xml file

galbramc commented 2 years ago

Does it add the xml extension or should it be --junitxml=test_reports.xml to get the extension?

pgkirsch commented 2 years ago

Ah it should be --junitxml=test_reports.xml

galbramc commented 2 years ago

test this please

galbramc commented 2 years ago

test this please

galbramc commented 2 years ago

Ok the scripts and jenkins are updated to use pytest now, and it seems to be working.

pgkirsch commented 2 years ago

Awesome thank you!

pgkirsch commented 2 years ago

@bqpd (or @whoburg) mind giving this a review when you get a chance? or I can just merge..

pgkirsch commented 2 years ago

test this please

whoburg commented 2 years ago

👏