Plugins add extra features to pytest without changing its core. They help you test better and faster.
You want to add new test report formats like HTML or XML.
You need to run tests in parallel to save time.
You want to check code coverage during tests.
You want to integrate pytest with other tools like Django or Flask.
You want to customize test discovery or add new command line options.
Syntax
PyTest
pip install pytest-plugin-name
# To list installed plugins
pytest --trace-config
# To uninstall a plugin
pip uninstall pytest-plugin-name
Replace pytest-plugin-name with the actual plugin name.
Use pip commands in your terminal or command prompt.
Examples
This installs the pytest-cov plugin to measure test coverage.
PyTest
pip install pytest-cov
This command shows all plugins pytest has loaded and their settings.
PyTest
pytest --trace-config
This removes the pytest-cov plugin from your system.
PyTest
pip uninstall pytest-cov
Sample Program
This example shows how to install the pytest-html plugin, run tests to generate an HTML report, and list all active plugins.
PyTest
# Step 1: Install a plugin# Run in terminal:
pip install pytest-html
# Step 2: Run tests with the plugin to create an HTML report
pytest --html=report.html
# Step 3: Check installed plugins
pytest --trace-config
OutputSuccess
Important Notes
Always check plugin compatibility with your pytest version.
Use virtual environments to avoid conflicts between plugins.
Some plugins add new command line options; read their docs to use them well.
Summary
Plugins extend pytest features without changing its core.
Install plugins using pip install and remove with pip uninstall.
Use pytest --trace-config to see active plugins and their settings.
Practice
(1/5)
1. What is the primary purpose of plugins in pytest?
easy
A. To add extra features without changing pytest's core code
B. To replace pytest's core functionality completely
C. To uninstall pytest from the system
D. To write test cases automatically
Solution
Step 1: Understand what plugins do in pytest
Plugins add new features or extend existing ones without modifying pytest's main code.
Step 2: Compare options with this understanding
Only To add extra features without changing pytest's core code correctly describes this purpose; others describe unrelated or incorrect actions.
Final Answer:
To add extra features without changing pytest's core code -> Option A
Quick Check:
Plugins extend features = C [OK]
Hint: Plugins add features without changing core code [OK]
Common Mistakes:
Thinking plugins replace pytest core
Confusing plugins with uninstall commands
Believing plugins auto-write tests
2. Which command correctly installs a pytest plugin named pytest-cov?
easy
A. pytest install pytest-cov
B. pip install pytest-cov
C. pip uninstall pytest-cov
D. pytest --install pytest-cov
Solution
Step 1: Identify the standard way to install Python packages
Python packages, including pytest plugins, are installed using pip install package-name.
Step 2: Match the command to install pytest-cov
Only pip install pytest-cov uses the correct pip install syntax; others are invalid commands.
Final Answer:
pip install pytest-cov -> Option B
Quick Check:
Use pip install for plugins = A [OK]
Hint: Use 'pip install plugin-name' to add plugins [OK]
Common Mistakes:
Using pytest commands to install plugins
Confusing uninstall with install
Adding extra flags not supported
3. What will be the output of the command pytest --trace-config when run in a project with plugins installed?
medium
A. The pytest version number only
B. An error saying the command is invalid
C. A list of all test cases found in the project
D. A list showing all active plugins and their configuration details
Solution
Step 1: Understand the purpose of pytest --trace-config
This command shows detailed information about pytest's configuration, including active plugins and their settings.
Step 2: Evaluate the options based on this purpose
A list showing all active plugins and their configuration details matches the expected output; others describe unrelated outputs or errors.
Final Answer:
A list showing all active plugins and their configuration details -> Option D
Quick Check:
--trace-config shows plugins = A [OK]
Hint: Use --trace-config to see active plugins and settings [OK]
Common Mistakes:
Expecting test case list from --trace-config
Thinking it shows version only
Assuming command causes error
4. You tried to uninstall a pytest plugin using pip uninstall pytest-cov but the plugin still appears in pytest --trace-config. What is the most likely cause?
medium
A. The plugin cannot be uninstalled once installed
B. You need to restart your computer to complete uninstallation
C. You have multiple Python environments and uninstalled in the wrong one
D. The command should be pytest uninstall pytest-cov
Solution
Step 1: Understand Python environments and plugin installation
Python packages can be installed in different environments (like virtualenvs). Uninstalling in one environment doesn't affect others.
Step 2: Analyze why plugin still appears after uninstall
If the plugin still shows, likely it was uninstalled in a different environment than the one pytest uses.
Final Answer:
You have multiple Python environments and uninstalled in the wrong one -> Option C
Quick Check:
Multiple envs cause uninstall confusion = B [OK]
Hint: Check Python environment when uninstalling plugins [OK]
Common Mistakes:
Believing plugins can't be uninstalled
Thinking restart is needed
Using wrong uninstall command
5. You want to install a plugin and ensure it is active for your pytest runs without affecting other projects. Which approach is best?
hard
A. Create a virtual environment, activate it, then run pip install plugin-name
B. Install the plugin globally using pip install plugin-name without a virtual environment
C. Copy plugin files manually into your project folder
D. Run pytest --install plugin-name inside your project
Solution
Step 1: Understand isolation of plugin installations
Using a virtual environment keeps plugin installations isolated per project, avoiding conflicts.
Step 2: Evaluate installation methods
Create a virtual environment, activate it, then run pip install plugin-name uses a virtual environment and pip install, which is best practice. Others either install globally or use invalid methods.
Final Answer:
Create a virtual environment, activate it, then run pip install plugin-name -> Option A
Quick Check:
Use virtualenv for isolated installs = D [OK]
Hint: Use virtualenv to isolate plugin installs per project [OK]
Common Mistakes:
Installing plugins globally causing conflicts
Trying to copy plugin files manually
Using invalid pytest commands to install
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.