Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

unittest -> pytest #425

Merged
merged 12 commits into from
Apr 15, 2020
Merged

unittest -> pytest #425

merged 12 commits into from
Apr 15, 2020

Conversation

cjermain
Copy link
Contributor

@cjermain cjermain commented Apr 6, 2020

This PR migrates the testing from unittest to pytest. This allows for the use of parametrized tests and fixtures, leading to more robust test coverage and easier test-code maintenance. This was motivated by my recent PRs on Bidirectional and Masking features, where I saw that many of the tests rely on nested for-loops, which are hard to filter (using -k) and maintain as they expand.

Goals:

  • Replace nested for-loops with parametrized tests
  • Replace test running functions with fixtures
  • Improve access to actual predictions and better integrate into pytest/pdb
  • Expand test coverage with additional parametrized tests

This PR is still a work in progress, and will be updated. I'm interested in any suggestions or comments.

@wenbingl
Copy link
Member

wenbingl commented Apr 8, 2020

Glad to see the effort, does pytest support the Test class as well?

@cjermain
Copy link
Contributor Author

Thanks. Pytest uses functions instead of test classes. Fixtures in pytest allow for setup/teardown of test resources, and can be composed to handle more complex situations. I am most interested in using parametrized tests so that we can loop over a large number of cases to get better test coverage.


def test_rnn_state_passing(self):
K.clear_session()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being run for all tests now in the runner fixture. It helps avoid difficult to reproduce errors that occur due to resource leaking in the tests.

@cjermain cjermain marked this pull request as ready for review April 13, 2020 01:14
@cjermain
Copy link
Contributor Author

This PR is ready for review. All of the main tests are converted to using pytest. There are a number of additional goals above, but given the diff already, I think they are better left for other PRs. This includes migrating the nightly tests, which still use unittest.

I would highly recommend using the "Hide whitespace changes" to review this PR, since the majority of the changes are indentation.
Screenshot_2020-04-12  WIP  unittest - pytest by cjermain · Pull Request #425 · onnx keras-onnx

@wenbingl & @jiafatom, can you review this PR?

@cjermain cjermain changed the title [WIP] unittest -> pytest unittest -> pytest Apr 13, 2020
Copy link
Member

@wenbingl wenbingl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you run code format to format the code before merge? like 'Reformat code' in pycharm.

@@ -0,0 +1,32 @@
###############################################################################
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the meaning of 'conftest'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conftest.py is the way that pytest shares fixtures across the entire test suite.
https://docs.pytest.org/en/latest/fixture.html#conftest-py-sharing-fixture-functions

if __name__ == "__main__":
unittest.main()
@pytest.mark.skipif(keras2onnx.proto.tfcompat.is_tf2 and is_tf_keras, reason="Tensorflow 1.x only tests.")
@pytest.mark.skipif(is_tf_keras and StrictVersion(tf.__version__) < StrictVersion("1.14.0"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StrictVersion(tensorflow.version.split('-')[0]) >= StrictVersi...
otherwise, the whole test cases will failed on tensorflow nightly build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by what you mean here. The current conditional is the same as that in line 133, so the previous behavior is reproduced. Was the original incorrect?

Copy link
Member

@wenbingl wenbingl Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code is also ‘imprecise’, but the issue was hidden because this condition wasn't tested since the first is_tf2 test is already false. And it looks the pytest.mark will test both. If you install a tensorflow nightly build. you will see the failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense. I've made the change.

self.assertTrue(opset_comparison)


if __name__ == '__main__':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we sometimes run this single test file with python cmd line instead of pytest, which helps on debugging. Is there alternative way in the pytest as well?

Copy link
Contributor Author

@cjermain cjermain Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can select a specific file by running pytest against that file.

pytest tests/test_misc.py -vv

You can also filter tests for specific strings of interest (e.g. LSTM):

pytest tests -k LSTM -vv

https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests

@cjermain
Copy link
Contributor Author

Can you run code format to format the code before merge? like 'Reformat code' in pycharm.

I use vim and don't have a formatter built in to my editor. There are a number of CLI formatting tools out there, for example black, yapf, and autopep8. Can you be more specific about the formatting you are looking for?

@wenbingl
Copy link
Member

Can you run code format to format the code before merge? like 'Reformat code' in pycharm.

I use vim and don't have a formatter built in to my editor. There are a number of CLI formatting tools out there, for example black, yapf, and autopep8. Can you be more specific about the formatting you are looking for?

I saw there were few PEP8 suggestions in my editor tool. But it's not mandatory now. Feel free to ignore it if it is not straightforward in your development.

@wenbingl
Copy link
Member

Thanks a lot for your contribution!

@wenbingl wenbingl merged commit f28b3a3 into onnx:master Apr 15, 2020
@cjermain cjermain deleted the feature/pytest branch April 16, 2020 00:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants