PyTest not discovering new test module, but changes made in old test modules are reflected in execution

Viewed 1654

(This has been fixed, please read the edit at the bottom even if nobody submits an answer)

I am writing tests for an app using PyTest, and had three test modules as of this evening. They work as expected, and any changes I make to them are reflected in the test execution (I intentionally put incorrect assertions to be completely sure). However, I added a new module this evening (which also begins with test_ and does not contain a class (so this SO answer does not apply), it is in fact almost identical in structure to one other test module) and no matter what I do PyTest refuses to discover it. I am using VS Code for development, but I tried running pytest from the terminal as well, to the same effect.

This issue on GitHub suggested I try rm -rf .cache which solved nothing. I tried removing all my __pycache__ files just to be safe as well as the .pytest_cache file, to no avail. I checked the contents of the .pytest_cache file after removing and running pytest again, and none of the new tests are shown in the nodeids. I do not get any errors, in particular test_grader.py, test_misc.py and test_solution_optimal.py work perfectly fine. Here is proof haha. test_solution_student.py is an exact copy of test_solution_optimal.py with a few deliberately incorrect assertions, but pytest won't discover it no matter what I do. Additionally, the __pycache__ in the tests directory contains a .pyc file corresponding to test_solution_student.py, which is just plain strange to me. I even tried renaming the file to something different thinking the discovery might be ignoring the second test because they both start with test_solution_, but nope.

I only enabled pytest as recommended here and the autoTestDiscover.... is enabled as well. However since running pytest from the terminal does not result in the discovery of the last module either I doubt it's an issue with my VS code settings file.

Directory structure:

Top-Level Directory
├── .pytest_cache
│   └── v
├── Grader
│   ├── (Several files to be tested)
│   ├── __init__.py
│   └── __pycache__
├── __pycache__
│   └── globals_store.cpython-37.pyc
├── graphs
│   ├── (Additional irrelevant files)
├── tests
│   ├── __pycache__
│   ├── test_grader.py
│   ├── test_misc.py
│   ├── test_solution_optimal.py
│   └── test_solution_student.py
└── venv
    ├── bin
    ├── include
    ├── lib
    └── share

I run pytest from the Top-Level Directory.

If somebody could give me a lead on what else I could try that might result in the test module being discovered I would forever be in your debt. I am not supposed to share the project since this is part of a homework assignment I am creating, but I am working on reproducing this issue in a dummy project.

Edit:

I "fixed" it; the issue lay with me duplicating the test_solution_optimal file, (duplicating code did smell funny to me, should have known to trust my intuition but I couldn't use that code out of the box and needed to make subtle modifications) which resulted in me redefining a fixture which I have now moved to a conftest.py file (I'm new to pytest, please forgive). I now know not to duplicate code (like, ever) but I am still curious as to why this was causing an issue which is why I chose not to add this as an answer to my question. If somebody could chime in with an explanation, be my guest :D

0 Answers
Related