如何在pytest中执行类内测试?及测试未被识别问题排查
Hey there, let’s work through why your pytest isn’t picking up those class-based tests! You’ve already covered the basics (following naming conventions, using an empty __init__.py), so let’s dive into some less obvious checks to fix this:
First, let’s clear up the __init__.py concern
Empty __init__.py files are completely fine—they only exist to mark a directory as a Python package, and you don’t need to add any code there for pytest to recognize tests. That’s not the root of your issue here.
Key Checks to Get Your Tests Recognized
Remove any custom
__init__method from your test class
Pytest skips test classes that define their own constructor. If your test class looks like this:class TestMyFeature: def __init__(self): self.setup_value = 10Delete that
__init__method—pytest handles instantiating test classes automatically, so you don’t need to define it.Make sure you’re running pytest from the right directory
If your project structure looks something like this:my_project/ tests/ __init__.py test_my_module.py src/ ...Run
pytestfrom the project root directory (my_project/), not inside thetests/folder. Running from the wrong path can prevent pytest from scanning all test files properly.Confirm your pytest environment matches your project interpreter
It’s common to have pytest installed in a different Python environment than the one you’re using for your project. Run these quick checks:pytest --versionto see which pytest executable is being usedwhich python(orwhere pythonon Windows) to confirm your active interpreter pathpip list | grep pytestto ensure pytest is installed in your current environment
Use pytest’s collect-only mode to debug discovery
Runpytest --collect-only -v—this command lists all tests pytest finds without running them. Look for your test class and method in the output:- If they show up: The issue is likely with how you’re triggering tests (e.g., missing flags)
- If they don’t show up: There’s a problem with discovery rules for that specific test
Check for conflicting configuration files
If you have apytest.ini,setup.cfg, ortox.iniin your project, it might override default discovery rules. Look for settings like:python_classes: Restricts which class names are treated as test classespython_functions: Limits recognized test method namestestpaths: Restricts which directories pytest scans
Temporarily rename these files (e.g.,pytest.ini.bak) and runpytest --collect-onlyagain to see if tests are detected.
Target the test directly to isolate the issue
Try running a specific test manually to see if pytest can find it:pytest tests/test_your_module.py::TestYourClass::test_your_method -vIf this works, pytest can see the test, but automatic discovery is blocked by something else (like config or directory structure).
Final Note
If none of these steps resolve the issue, sharing the output of pytest -v and a text-based breakdown of your directory structure would help narrow things down even faster!
内容的提问来源于stack exchange,提问作者Prabodh Ghosh

