Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Run Python Tests in PyCharm: From Single Test to Full Coverage

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to run Python tests in PyCharm, starting with a single test method and scaling up to running your entire test suite with coverage reports. You'll learn the exact clicks and shortcuts for PyCharm's built-in test runner, how to configure coverage, and how to interpret the results. Whether you're using pytest, unittest, or another framework, these steps work for the most common setups.

What You'll Need

  • PyCharm (Community or Professional edition) installed, version 2021.1 or newer.
  • A Python project with test files that PyCharm can detect (files named test_*.py or *_test.py, or classes inheriting from unittest.TestCase).
  • A test framework configured: PyCharm supports pytest, unittest, and nose by default. If you use pytest, make sure it's installed in your project's interpreter (pip install pytest).
  • For coverage, the coverage.py package is required. PyCharm can install it automatically when you first run with coverage.

Step-by-Step

  1. Open your project and locate the test file. In the Project tool window (usually on the left), navigate to the test file you want to run. PyCharm recognizes test files by their names and structure.
  2. Run a single test method. Open the test file in the editor. In the gutter (the narrow column to the left of the code), you'll see a green play icon next to each test method (functions starting with test_ or methods in a unittest class). Click that icon and select Run 'pytest test_example' (or the appropriate runner name). The test runs in the Run tool window at the bottom.
  3. Run an entire test class or module. To run all tests in a class, click the play icon next to the class declaration. To run all tests in a file, click the play icon next to the file name in the Project tool window, or open the file and use the shortcut Ctrl+Shift+F10 (Windows/Linux) or Cmd+Shift+F10 (macOS) to run the current file's tests.
  4. Run all tests in the project. Right-click the project root folder in the Project tool window and select Run All Tests. Alternatively, open the Test Runner panel (View > Tool Windows > Test Runner) and click the green play button in its toolbar to run all discovered tests.
  5. Run tests with coverage. To see which lines of code your tests execute, right-click the project root (or a specific folder/file) and select Run with Coverage. PyCharm will prompt you to install the coverage.py package if it's missing. After the run, the Coverage tool window opens, showing percentages per file and highlighting covered/uncovered lines in the editor (green for covered, red for uncovered).
  6. View the coverage report. In the Coverage tool window, you can expand packages and modules to see coverage percentages. Click a file to see line-by-line highlighting. The toolbar lets you export the report (e.g., as HTML) or jump to the next uncovered line.
  7. Configure test runner defaults. Go to Settings/Preferences > Tools > Python Integrated Tools. Under Testing, choose the default test runner (pytest, unittest, etc.). This ensures the run buttons use your preferred framework. For coverage, you can set options like branch coverage under Settings > Build, Execution, Deployment > Coverage.
  8. Run a subset of tests using markers or expressions. If you use pytest, you can run tests matching a pattern. In the Run configuration (select Edit Configurations from the run dropdown), set the Additional Arguments field to something like -k "test_login or test_logout" to run only tests matching those names. For unittest, you can use python -m unittest test_module.TestClass.test_method as a custom configuration.

Common Mistakes

  • Not configuring the test runner. If PyCharm doesn't recognize your tests, it may be because the default runner is set to something else. Always check the Python Integrated Tools settings.
  • Running tests with the wrong interpreter. Tests may fail with ModuleNotFoundError if the selected Python interpreter doesn't have the required packages. Ensure the project interpreter is correct in Settings > Project > Python Interpreter.
  • Ignoring coverage output. Coverage only shows lines that were executed. If you see low percentages, it might be that your test setup/teardown methods aren't covering enough, or you're missing edge cases.
  • Using the wrong run button. Clicking the play icon next to a file runs the whole file, not just the method you're looking at. Be precise about which icon you click.
  • Forgetting to save test files. PyCharm runs the saved version of your code. If you have unsaved changes, the test may run against old code. Always save (Ctrl+S) before running.

Tips

  • Use keyboard shortcuts: Ctrl+Shift+F10 runs the current test file, and Ctrl+Shift+R runs with coverage (on Windows/Linux; replace Ctrl with Cmd on macOS).
  • Create a dedicated run configuration for your full suite: go to Run > Edit Configurations, add a Python test configuration, set the target to the project root, and save it. This makes it one click to run everything.
  • Use coverage to find dead code: lines that are never covered might be unreachable or outdated. But remember, 100% coverage doesn't guarantee bug-free code—it only shows which lines were executed.
  • For pytest, you can use --cov flags in the additional arguments if you prefer command-line style, but PyCharm's built-in coverage is more visual.
  • If you have multiple test files, use the Test Runner tool window to see a tree of all tests and run them individually by right-clicking.

Troubleshooting

  • Tests are not discovered. Make sure your test files are in a folder that PyCharm marks as a source root. Right-click the folder > Mark Directory as > Sources Root. Also check that the test file names match the patterns (test_*.py or *_test.py).
  • Run button is grayed out. This usually means PyCharm doesn't recognize the file as a test file. Verify the file name and content. If you're using pytest, ensure pytest is installed in the interpreter.
  • Coverage reports 0%. Check that you ran with coverage (not just a regular run). Also ensure the code you're testing is in the same project and not in a separate package that isn't included in the coverage scope.
  • Import errors when running tests. This often happens when the working directory is wrong. In the run configuration, set the working directory to the project root. Also check that your project structure is correct (e.g., using relative imports).
  • Tests pass in terminal but fail in PyCharm. The PyCharm run environment may have different environment variables. Check the run configuration's environment variables and compare with your terminal.
  • Coverage tool window is empty. Try running coverage again after cleaning the previous report. In the Coverage tool window, click the broom icon to clear, then rerun.

Frequently Asked Questions

Q: Can I run a single test case with a specific parameter?
A: Yes, if you're using pytest with parametrize, the gutter icon appears next to each parameterized test. Click it to run that specific instance. For unittest, you can use subTest but running a single subtest isn't directly supported; you'd need to run the whole method.

Q: How do I run tests from the command line inside PyCharm?
A: Open the terminal tool window (View > Tool Windows > Terminal) and run python -m pytest or python -m unittest. The terminal uses the project's interpreter by default.

Q: What's the difference between 'Run' and 'Run with Coverage'?
A: 'Run' executes the tests and shows pass/fail. 'Run with Coverage' does the same but also instruments the code to measure which lines are executed, providing a coverage report.

Q: Can I see coverage for multiple test runs combined?
A: Yes. In the Coverage tool window, click the folder icon to load a previous coverage report, then run another coverage and choose 'Add to active suite' when prompted. This combines results.

Q: Why does PyCharm say 'No tests were found'?
A: This usually means the test runner is misconfigured or the test file doesn't match the pattern. Check the test runner setting and file naming. Also ensure the file is not excluded from the project.

Q: How do I exclude certain files from coverage?
A: In the Coverage tool window, right-click and select 'Exclude from Coverage'. This adds the file to an exclusion list that you can manage in Settings > Build, Execution, Deployment > Coverage.