Specifying Test Directory Locations

Whereas norecursedirs tells pytest where not to look, testpaths tells pytest where to look. testspaths is a list of directories relative to the root directory to look in for tests. It’s only used if a directory, file, or nodeid is not given as an argument.

Suppose for the Tasks project we put pytest.ini in the tasks_proj directory instead of under tests:

 tasks_proj/
 ├── pytest.ini
 ├── src
 │   └── tasks
 │   ├── api.py
 │   └── ...
 └── tests
  ├── conftest.py
  ├── func
  │   ├── __init__.py
  │   ├── test_add.py
  │   ├── ...
  └── unit
  ├── __init__.py
  ├── test_task.py
     └── ...

It could then make sense to put tests in testpaths:

 [pytest]
 testpaths = ​tests

Now, as long as you start pytest from the tasks_proj directory, pytest will only look in tasks_proj/tests. My problem with this is that I often bounce around a test directory during test development and debugging, so I can easily test a subdirectory or file without typing out the whole path. Therefore, for me, this setting doesn’t help much with interactive testing.

However, it’s great for tests launched from a continuous integration server or from tox. In those cases, you know that the root directory is going to be fixed, and you can list directories relative to that fixed root. These are also the cases where you really want to squeeze your test times, so shaving a bit off of test discovery is awesome.

At first glance, it might seem silly to use both testpaths and norecursedirs at the same time. However, as you’ve seen, testspaths doesn’t help much with interactive testing from different parts of the file system. In those cases, norecursedirs can help. Also, if you have directories with tests that don’t contain tests, you could use norecursedirs to avoid those. But really, what would be the point of putting extra directories in tests that don’t have tests?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.247