Mongoengine: Switch test suite from nose to pytest

Created on 29 Jun 2019  ·  9Comments  ·  Source: MongoEngine/mongoengine

Unless you are a huge nosetest fan, I'd suggest to switch the test suite to pytest. I'm pretty sure you are already familiar with pytest @wojcikstefan but if not, or if you need me to elaborate on why it's better, just let me know :)

Code Quality High Priority

Most helpful comment

Leave that with me, I should have some time to work on this in the next weeks

All 9 comments

Thanks for opening an issue about this @bagerard. I'm definitely in favor of switching our test suite to pytest :)

Next steps:

  1. Make sure all of our tests are collected.

    • Currently most of our test files have a test_ prefix in their name and hence are automatically discovered by pytest. However, there are some exceptions that we'd need to fix (e.g. tests/queryset/queryset.py would have to be renamed to tests/queryset/test_queryset.py).

    • An alternative naming convention would be to make pytest collect all tests/*.py files and then we could drop the test_ prefix altogether. I've worked with codebases using this convention and I'm quite fond of it actually – it's nice when testing of lib_name/x/y/z.py is done in tests/x/y/z.py (as in, tests mirror the structure of the code exactly). The drawback here is that test-specific utility files might be harder to discern from files containing actual tests. That said, in practice I've noticed that it doesn't matter much since you typically only have the standard conftests.py and fixtures.py (or a fixtures/*.py dir).

  2. Make Travis install pytest and change tox.ini to use pytest instead of python setup.py nosetests.
  3. Replace unittest-style tests with pytest-style tests w/ fixtures, regular asserts, etc.

    • This might require a LOT of work, so we can probably do it over time. That is, as we work on other features/bugs and as we touch tests, we convert these tests to pytests.

Does that make sense @bagerard? Do you have a preference wrt the naming convention of test files? Am I missing anything?

THanks for your feedback @wojcikstefan, glad to hear that you like it as well. Personally I prefer to use the "test_{original_filename}.py" convention for test files (being the default of pytest), I like that it makes test files easier to find in IDE but it doesn't matter much at the end, just tell me which way you want to go.

regarding 3) the pytest guys actually provide unittest2pytest that ease the migration (smart move from them)

I agree that in terms of planning, we should decouple:

  • keeping test suite +- as it is and move to pytest including in travis, since pytest supports unittest (to some extent), it shouldn't be interfering with other PRs
  • convert all asserts, etc. With the help of the package above --> I'd try to do this is in 1 shot

I like that it makes test files easier to find in IDE

That's a valid point. Given that a) this is the pytest default, b) most of our test suite already conforms to this convention, c) it helps with file navigation in some IDEs, I'm happy to go the test_{original_filename}.py route 👍

I'd try to do this is in 1 shot

SGTM. Wanna give it a try or should I?

Leave that with me, I should have some time to work on this in the next weeks

I'm working on this right now, I struggled with getting it working with 2to3 + tox but its moving forward, almost there

Current status:

  • [x] Rename all test files to test_{orginal_filename}.py #2156
  • [x] Switch test suite runner from nose to pytest and plug coverage (#2157)
  • [x] Convert all legacy assertions (self.assertEqual) to (pytest) natural assertions (ready in my fork https://github.com/bagerard/mongoengine/pull/13)

@bagerard you may have luck with https://github.com/pytest-dev/unittest2pytest for your third TODO

Yep, thanks I actually already started reworking the assertions and that lib was amazing, worked like a charm :)

Closing this as the switch was done recently

Was this page helpful?
0 / 5 - 0 ratings