Pytest tutorial: Pytest installation and configuration under Windows

In the field of software development, testing is the key to ensuring software quality and stability. Pytest is a full-featured testing framework based on Python, which provides a simpler, more readable, and more flexible way to write test cases, and can easily manage the test process, generate reports, support fixtures and plug-ins, and other functions. In this article, we will introduce how to install and configure Pytest under Windows, and use PyCharm for development.

1. Install pytest

  1. Install Python

First, you need to install the Python environment. In the Windows system, you can download the latest version of the Python installation package from the Python official website (https://www.python.org/downloads/windows/) for installation. During installation, it is recommended to add Python to the system environment variables so that the Python interpreter can be accessed in any directory.

       

    2. Install Pytest

There are many ways to install Pytest, one of which is to use the pip command to install. Enter the following command on the command line:

pip install pytest

   3. After the installation is complete, you can use the following command to verify whether Pytest has been installed successfully: 

pytest --version

2. Create a virtual environment In order to ensure that the version of Python's dependent library is consistent and avoid conflicts with the system environment, it is recommended to use a virtual environment. On Windows, you can use the venv module to create a virtual environment. The operation steps are as follows:

  1. Open the command line tool and enter the directory where the virtual environment is to be created.
  2. Run the following command to create a virtual environment named "venv" in the current directory.
python -m venv venv

     3. Activate the virtual environment. Enter the following command in the command line tool:

venv\Scripts\activate

 After activating a virtual environment, the prompt at the command line displays the name of the virtual environment.

    4. Use the pip command to install dependent libraries such as Pytest. For example:

pip install pytest

    5. When the virtual environment is no longer needed, use the following command to stop the virtual environment:

deactivate

 

3. Configure Pytest Before writing test cases, you need to configure Pytest, including specifying test files, selecting the execution mode of the test, etc., to ensure that the test cases are run correctly. Here's how to configure Pytest:

  1. Run the following command in the virtual environment to generate the pytest.ini file:
pytest --genscript=pytest.ini

     2. Modify the content of the pytest.ini file, specify the directory or file to be tested, set the log output, associate plugins, etc., for example:

[pytest]
addopts = -s -q --alluredir allure-report
testpaths = tests

log_cli = true
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
log_file = logs/test.log
log_file_level = INFO
log_file_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)

junit_family = xunit2

markers =
    smoke: run the smoke test cases
    regression: run the regression test cases
    performance: run the performance test cases
  • addopts: used to specify pytest command line parameters. The meaning of the above command line parameters:
    • -s: Print the print and logging information in the test function.
    • -q: Simplify the output, only show valid information.
    • --alluredir: Specifies the directory where allure reports are generated.
  • testpaths: Specify the directory or file to run the test case.
  • log_cli: Whether to output logs to the console.
  • log_cli_level: Log level for console output.
  • log_cli_format: The log format for console output.
  • log_file: Output logs to a file.
  • log_file_level: The level of log output to file.
  • log_file_format: The format of the log output to the file.
  • junit_family: Specifies the JUnit XML output format.
  • markers: A collection of marker test functions.

4. Use PyCharm to configure the test environment PyCharm is a powerful Python IDE that provides many convenient tools to write and execute Python code. Here's how to configure the test environment in PyCharm:

  1. Open PyCharm, click File -> New Project in the menu bar, and create a new project.
  2. In the created project, right-click, select New -> Python File in the pop-up window, name the file "test_example.py", and write a sample test case in this file.
def test_addition():
    assert 2 + 3 == 5

def test_subtraction():
    assert 5 - 3 == 2

 

    3. Install the Pytest plugin in PyCharm. Click File -> Settings in the menu bar, select Plugins in the pop-up window, search for Pytest, and click Install to install the Pytest plugin.

    4. Configure the test environment of PyCharm. Open Run -> Edit Configurations in the menu bar. In the pop-up window, select Python tests, click the Add button (+) in the upper right corner, and then select pytest.

    5. Enter the path of the test file to be run in "Script Path", for example:

 

D:\project\tests\test_example.py

 

Select the Python interpreter, here choose to use the interpreter in the virtual environment, ie venv\Scripts\python.exe.

    6. Enter the parameters that need to be passed to Pytest in "Additional Arguments", for example:

-s -v

 

    7. Click OK to save the configuration. Pytest test cases can now be run in PyCharm using Run -> Run 'pytest in test_example.py' from the menu bar.

V. Summary

In this article, we introduced how to install and configure Pytest under Windows system, and create a virtual environment to ensure the version consistency of Python dependent libraries. We also covered how to configure Pytest and integrate Pytest in PyCharm for testing. I hope this article can help readers better understand and master the relevant knowledge of Pytest.

Guess you like

Origin blog.csdn.net/weixin_40025666/article/details/131142465