pytest_sessionstart in conftest.py not running when conftest.py isn't in main testing directory

Viewed 426

My project structure:

cool_project/
   tests/
      cool_dir/
         test_cool.py
         conftest.py

      test_main.py

I'm using pytest version 6.2.1, and python 3.8.7. I have a conftest.py, containing pytest_sessionstart. If conftest.py isn't in the root for my testing directory, which is the directory tests, then pytest_sessionstart won't run (for example, if it is in tests/cool_dir). But, if I move conftest.py to the main directory, which is tests, then it works. So how do I make pytest_sessionstart run, when conftest.py isn't in the tests directory? I currently have the code: tests/cool_dir/conftest.py

print("CONFTEST ENTERED")

def pytest_sessionstart(session):
    print("pytest_sessionstart has been called")

tests/cool_dir/test_cool.py

def test_bar():
    assert True

tests/test_main.py

def test_foo():
    assert True

"CONFTEST ENTERED" is the only thing that is displayed when I run pytest -s

You can test the code on my repl.it repl.

0 Answers
Related