-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🧪 TESTS: convert AiidaTestCase
-> pytest
#4782
Conversation
Process: - unittest2pytest -n -w path/to/file - add `# pylint: disable=no-self-use` - replace `AiidaTestCase` with `@pytest.mark.usefixtures('clear_database_before_test_class')` - remove from `aiida.backends.testbase import AiidaTestCase` - change @unittest.skip -> @pytest.mark.skip, @unittest.skipIf -> @pytest.mark.skipif The were some issues where`assertAlmostEquals` had been used for strange cases Annoyingly there are some limitations, one being that the class fixture is called AFTER a `setup_class` method (see https://stackoverflow.com/questions/31484419/pytest-setup-class-after-fixture-initialization_) and also class methods don't accept fixtures. This makes it difficult to do a 1-to-1 replacement of any `setUpClass` methods used, and also replace the use of `self.computer`
cc @ltalirz, this should pass all tests for the files I have changed (plenty more to do), so if you want to have a look do a quick "pre-review" for any issues, before I apply this to any more files? |
AiidaTestCase
-> pytest
Codecov Report
@@ Coverage Diff @@
## develop #4782 +/- ##
===========================================
- Coverage 79.56% 79.52% -0.04%
===========================================
Files 515 515
Lines 36812 36817 +5
===========================================
- Hits 29285 29274 -11
- Misses 7527 7543 +16
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @chrisjsewell - looks good to me, no obvious issues come to mind.
when transforming more tests you may run into the fact that the AiidaTestCase is a bit more rigorous in cleaning not only the db but also resetting various caches (see the clean_db
method).
These may need to be ported over to the reset_db
method of the ProfileManager
I guess this can then also be closed? |
Yep 👍, superseded by #5372 Perhaps some of the tips for writing tests, in the initial comment, should be copied somewhere? |
Conversion process:
unittest2pytest -n -w path/to/file
(see https://github.com/pytest-dev/unittest2pytest)# pylint: disable=no-self-use
AiidaTestCase
with@pytest.mark.usefixtures('aiida_profile_clean_class')
(I would say this is not recommended unless there are particular performance reasons, since every test should be independent),@pytest.mark.usefixtures('aiida_profile_clean')
, or using an autouse fixture (see below)def setUp(self):
withdef setup_method(self):
or an autouse fixturefrom aiida.backends.testbase import AiidaTestCase
@unittest.skip
->@pytest.mark.skip
,@unittest.skipIf
->@pytest.mark.skipif
The were some issues where
assertAlmostEquals
had been used for strange cases, related to this I also added an__abs__
method toNumericType
Annoyingly there are some limitations of using classes with pytest, one being that a class
usefixtures
decorator is called AFTER asetup_class
method (seehttps://stackoverflow.com/questions/31484419/pytest-setup-class-after-fixture-initialization_) so any nodes created in
setup_class
will be wiped from the database byaiida_profile_clean_class
.Also class methods do not accept fixtures.
This makes it difficult to do an exact 1-to-1 replacement of any
setUpClass
->setup_class
methods used, and also replace the use ofself.computer
edit: https://docs.pytest.org/en/latest/how-to/unittest.html#using-autouse-fixtures-and-accessing-other-fixtures allows for using fixtures per test, and so can solve the use of
self.computer
etc e.g.or if you don't need any instance attributes: