How do I configure PyCharm to run py.test tests?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
PyCharm is a popular integrated development environment (IDE) that offers a comprehensive suite of tools for Python development. One of its most powerful features is its ability to integrate directly with testing frameworks like `py.test` (now known as just `pytest`). This article will guide you through configuring PyCharm to seamlessly run `pytest` tests, illustrating the process with technical explanations and examples.
Prerequisites
Before configuring PyCharm for `pytest`, make sure you have the following:
- PyCharm IDE: Ensure that PyCharm is installed on your system. The following instructions are based on PyCharm Professional Edition, but similar steps should apply to the Community Edition.
- Python: A working Python environment set up on your system.
- Pytest: Install `pytest` if you haven't already by running `pip install pytest` in your terminal.
Step-by-Step Configuration
Step 1: Install Pytest
Since `pytest` isn't bundled with Python, you need to install it using pip:
- Navigate to `File > Settings` (on Windows/Linux) or `PyCharm > Preferences` (on macOS).
- Select `Project: <Your Project Name> > Python Interpreter`.
- Choose the appropriate interpreter from the list. If not present, add your Python path.
- Go to `File > Settings` (on Windows/Linux) or `PyCharm > Preferences` (on macOS).
- Under `Tools`, click on `Python Integrated Tools`.
- Look for the `Testing` section.
- From the `Default test runner` dropdown, select `pytest`.
- Click `Apply` and then `Ok` to confirm the changes.
- Using Context Menu:
- Right-click on the `test_example.py` file in the project explorer.
- Select `Run 'pytest in <your file>'`.
- Using the Run Menu:
- Open the `test_example.py` file.
- From the Run menu, choose `Run...` and select your test file.
- Go to `Run > Edit Configurations...` in the top menu.
- Click the `+` button and select `Python tests > pytest`.
- Name your configuration (e.g., `Run All Tests`).
- Set the `Target` to where your tests are located (e.g., a module or directory).
- Optional: Set additional options if needed, such as environment variables, working directory, etc.
- Choose your newly created configuration from the dropdown in the toolbar and click the Run button.
- Go to `File > Settings` and select `Project: <Your Project Name> > Python Interpreter`.
- Click on the gear icon next to the interpreter list and choose `Add`.
- Select `Existing environment` and navigate to the path of your virtual environment's Python executable.
Related reading
- How do I connect to a MySQL Database in Python?
- How do I connect to a MySQL Database in Python?
- How do I connect to a MySQL Database in Python?
- How do I convert a IPython Notebook into a Python file via commandline?
- How do I correctly setup and teardown for my pytest class with tests?
- How do I disable a test using pytest?
- How do I convert a numpy array to and display an image?
- How do I convert a Pandas series or index to a NumPy array?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.