You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm提示“No tests were found”,单元测试无法被检测问题求助

Fixing PyCharm's "No tests were found" Issue

Hey there, I totally get how frustrating it is to spend hours writing tests only for PyCharm to not detect them. Let’s walk through the most common fixes that usually resolve this headache:

  • Check naming conventions first
    PyCharm relies on standard naming rules to pick up tests by default:

    • Test files need to start with test_ (e.g., test_user_service.py)
    • Test methods/classes must also start with test_ (e.g., test_create_user() or class TestUserService:)
      If your files/methods don’t follow this pattern, PyCharm will skip over them.
  • Verify your test framework configuration

    1. Open PyCharm’s Settings (File > Settings, or press Ctrl+Alt+S on Windows/Linux, Cmd+, on Mac)
    2. Navigate to Tools > Python Integrated Tools
    3. Under the Testing section, make sure the default test runner matches the framework you’re using (e.g., pytest if you wrote pytest tests, unittest for standard library tests)
    4. If you’re using pytest, double-check that it’s installed in your project’s virtual environment (pip install pytest—PyCharm can’t detect the framework if it’s missing)
  • Mark test directories as Test Sources Root
    If your tests live in a subdirectory (like tests/), PyCharm might not scan it by default:

    • Right-click your test directory in the Project pane
    • Select Mark Directory as > Test Sources Root
      This tells PyCharm to prioritize scanning this folder for test files.
  • Clear PyCharm’s cache
    Cached data can sometimes break test detection. Try this:

    • Go to File > Invalidate Caches...
    • Select Invalidate and Restart
      This will wipe PyCharm’s cached data and restart the IDE—this fixes a surprising number of weird detection issues.
  • Check for syntax errors in your test code
    If your test files have syntax mistakes (like incorrect indentation, undefined variables, or missing imports), PyCharm can’t parse them properly, so it won’t detect any tests. Run your tests manually in the terminal first to confirm they’re valid:

    • For unittest: python -m unittest path/to/test_file.py
    • For pytest: pytest path/to/test_file.py
      Fix any errors before trying again in PyCharm.
  • Confirm your virtual environment is set up correctly
    Make sure PyCharm is using the same virtual environment where you installed your test framework:

    1. Go to Settings > Project: [Your Project Name] > Python Interpreter
    2. Check that pytest (if using it) is listed in the installed packages
    3. If you need to switch environments, use the dropdown menu to select the correct one, then restart PyCharm.

内容的提问来源于stack exchange,提问作者Jakub Gawel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:49:33