2 min read

Pytest 3.5 and --new-first option

Pytest 3.5 and --new-first option

Pytest 3.5 has released! Many console options have been added to this version. I strongly recommend to read about these options and apply them to your work πŸ™‚

New command-line options

These are some new features you can start using right now:

  • --show-capture – accepts no, stdout, stderr, log, or all (default) values. Now you can specify which output you want to see in the console.
  • --rootdir – allows you to customize the root directory
  • --nf, --new-first allows you to change the order of execution of tests and to perform new tests in the beginning
  • --last-failed-no-failures allows you to configure the behavior of the --last-failed option
  • --doctest-continue-on-failure – allows you not to stop on the first error and to continue the execution when you running doctest
  • --verbosity – this flag allows you to specify the level of detail explicitly

The β€œ--new-first” option

All options are noteworthy, but I would like to focus on one of them: --nf, --new-first.

How this option works: Pytest takes from cache the list of all tests from the previous run and compares with the current run. If he meets tests that were not in the previous runs, then puts in a queue ahead. Next, the test queue becomes in the order of aging changes to the test files.

For example, there are 2 files test_1.py and test_2.py with 2 tests in each file. Let’s say we just changed the file test_2.py and made changes in some test. If you run in normal mode, firstly will be added the tests from the test_1.py file, then from the test_2.py file.

$ pytest tests -v
======================================================= test session starts ========================================================
platform linux -- Python 3.6.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 -- /home/username/examples/env/bin/python
cachedir: .pytest_cache
rootdir: /home/username/examples, inifile: 
collected 4 items
tests/test_1.py::test_1 PASSED [ 25%]
tests/test_1.py::test_2 PASSED [ 50%]
tests/test_2.py::test_1 PASSED [ 75%]
tests/test_2.py::test_2 PASSED [100%]
===================================================== 4 passed in 0.02 seconds =====================================================

In case if you run pytest with --new-first option, the order will be changed, and firstly tests will run from the file test_2.py and then from the test_1.py.

$ pytest tests --nf -v
======================================================= test session starts ========================================================
platform linux -- Python 3.6.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 -- /home/username/examples/env/bin/python
cachedir: .pytest_cache
rootdir: /home/username/examples, inifile:
collected 4 items
tests/test_2.py::test_1 PASSED [ 25%]
tests/test_2.py::test_2 PASSED [ 50%]
tests/test_1.py::test_1 PASSED [ 75%]
tests/test_1.py::test_2 PASSED [100%]

===================================================== 4 passed in 0.02 seconds =====================================================

At the same time, if there is a new test, which was not in the previous run, it will be run at first place:

$ pytest tests --nf -v
======================================================= test session starts ========================================================
platform linux -- Python 3.6.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 -- /home/username/examples/env/bin/python
cachedir: .pytest_cache
rootdir: /home/username/examples, inifile:
collected 5 items
tests/test_2.py::test_3 PASSED [ 20%]
tests/test_2.py::test_1 PASSED [ 40%]
tests/test_2.py::test_2 PASSED [ 60%]
tests/test_1.py::test_1 PASSED [ 80%]
tests/test_1.py::test_2 PASSED [100%]

===================================================== 5 passed in 0.02 seconds =====================================================

Conclusion

Pytest is a very powerful tool for testing. If you still don't use it, I encourage you to try :)

What do you think about these features? Which features do you like the most in pytest? Please, share you thoughts with me!