'PyCharm: individually working tests, not finding a file when run in group

In my Python project, I have a set of unittests, that load some data from files in a data sub-directory.

In PyCharm (Community 2019.3), running each of them individually all of them work, but they fail when running them in group, through doing right-click in the 'tests' directory of the Project sidebar structure.

Dir structure (showing just 1 example):

Project dir structure

I load some mock files, as some .ini's: when executed individually, they work, when done in group, they fail. Output:

--- Logging error ---
Traceback (most recent call last):

Failure
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/suite.py", line 163, in _handleClassSetUp
    setUpClass()
  File "/home/.../tests/test_ConnectorUtils.py", line 22, in setUpClass
    with open("data/settings.ini") as fsettings:
FileNotFoundError: [Errno 2] No such file or directory: 'data/settings.ini'

When run individually:

Testing started at 16:07 ...
/usr/bin/python3.6
    /snap/pycharm-community/175/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py
    --path /home/.../tests/test_ConnectorUtils.py
Launching unittests with arguments python -m
    unittest /home/.../tests/test_ConnectorUtils.py in /home/.../tests



Ran 5 tests in 0.009s

OK

Not sure if it may be relevant, but before my files were not starting by test_, so, Pycharm was not detecting any tests, so, I renamed them, as it suggests in: Pycharm - no tests were found?

Config check I did:

Run Configurations

Following: PyCharm unittests only work individually

Python Integrated Settings

Following: Pycharm and unittest does not work

I cannot find any issue, do someone has any idea, please?



Solution 1:[1]

Working on Windows 10, what solved for me was to simply remove the '/tests' from my WORKING directory

Solution 2:[2]

When you run all the tests as a group, PyCharm launches _jb_pytest_runner.py with a different --path argument than it does when launching tests individually. This --path argument sets the working directory pytest is running in.

You should be able to see this printed out in the test/debug console when the tests startup. Something similar to below printing out the working directory.

Running individual tests prints something similar to:

Testing started at 1:50 PM ...

Launching pytest with arguments

C:/Users/username/PyCharmProjects/project_dir/test_dir/test_sub_dir/test --no-header --no-summary -q in C:\Users\username\PyCharmProjects\project_dir\test_dir\test_sub_dir\test

Running a group of tests prints something similar to:

Testing started at 1:58 PM ...

Connected to pydev debugger (build 213.6461.77)

Launching pytest with arguments --no-header --no-summary -q in C:\Users\username\PyCharmProjects\project_dir

The difference being the directory pytest is launching in.

To fix this for every time you run an individual test:

  1. Go to Run > Edit Configurations...
  2. In the new "Run/Debug Configurations" window, delete all your configurations under 'Python tests' (as these won't be updated correctly and will need to be recreated)
  3. Click "Edit configuration templates..." (located in the bottom left corner)
  4. In the new window ("Run/Debug Configuration Templates"), on the left side expand "Python tests"
  5. Select 'Autodetect'
  6. In the 'Working directory' field type or navigate to the path of your project folder (or whatever folder you want to have as your working directory each time pytest is running)
  7. Click Apply
  8. Click Ok
  9. (Now back in the "Run/Debug Configurations" window) Click Apply
  10. Click OK
  11. Update filepaths to test files in your test functions to be relative to the working directory you just set
  12. Recreate your test configurations and try running individual tests and group tests again, check the test console output again to make sure pytest is starting in the working directory you set

To fix it per individual test (as mentioned by PCamargo):

  • Change the working directory field in the "Run/Debug Configurations" window for your individual test configuration to whatever working directory is being run in your grouped pytest

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PCamargo
Solution 2