question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running individual test files doesn't work without conftest.py

See original GitHub issue

When I try and run an individual test without a conftest.py file in the root directory of my project, it appears that it’s not loading modules correctly. Here’s a screenshot of what I’m doing.

pytest_bug

The imports at the top of that test are:

import pytest
import datetime
import uuid
import kids.database as database

I would expect that pytest would have the same functionality without a conftest.py file as it would with an empty conftest.py file, but that appears not to be the case.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
The-Compilercommented, Aug 26, 2019

Note that the ModuleNotFoundError is probably the correct behavior here: The current directory isn’t necessarily in sys.path (where Python looks for modules) - it only happens to work because pytest adds it to load the conftest.py file.

IMHO, the proper solution is using a virtualenv where you have a development install of your project, and/or tox.

edit: @pfctdayelise was faster!

0reactions
The-Compilercommented, Aug 26, 2019

If you want this to throw an error, shouldn’t it also throw an error when conftest.py is found?

It should, which is why #2269 is open 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't pytest load conftest.py when running only a ...
I want to separate my test runs into unit tests and API tests, I don't want to run them all in one go....
Read more >
[SOLVED] PyCharm can no longer find conftest.py file for ...
I've been using PyCharm CE 2019.2.3 to run and debug tests for a library I'm developing. It's been working fine for a while,...
Read more >
Parametrizing tests — pytest documentation
pytest will build a string that is the test ID for each set of values in a parametrized test. These IDs can be...
Read more >
PyTest Tutorial: What is, How to Install, Framework, Assertions
To run tests only from a specific file, we can use py.test <filename> ... py.test -k methods -v – will not run any...
Read more >
Pytest best practices for libraries - Towards Data Science
py files in the conftest.py, leaving the fixtures which are specific to a test module in there. One potential drawback of this approach...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found