pytest is an automated testing framework;
tox can start a virtual environment (venv) and execute various scripts. It supports multiprocessing, which makes testing faster.
Together, pytest and tox allow you to create a virtual environment and then run pytest to execute test scripts.
Currently, the framework is configured to run tests with 8 threads by default.
Requirements:
-
Place all test code in the
testsdirectory, with files starting withtest_and test functions also starting withtest_. This is a pytest convention. -
Copy
tests/__init__.pyandtox.iniinto your new project. Make sure to keep thetests/__init__.pyfile; otherwise, errors may occur. -
Endpoint test functions should accept a
clientparameter and useget_data/post_datafunctions to simulate user GET/POST requests. Each endpoint should have at least one test function. -
By default, pytest uses the user 'gs1/gs1' for login and test execution. If you need to use another user, create a
tests/conftest.pyfile and add the following:import pytest @pytest.fixture(scope="session") def login_info(): return {'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD'}
-
Install
toxandpytest:pip3 install tox pytest
-
To run a single test file, execute the following command in the root directory:
pytest -e CONFIG -s --disable-warnings tests/xxx.py
-e: Loads a specific configuration file. For example,-e devloadsconfig/config_dev.py. (Since v2.5.0)-s: Displays console and logger output.--disable-warnings: Ignores warnings.
-
To run all test cases, simply execute
toxin the root directory.
Note: You do not need to start the service manually; the testing framework will handle it automatically.
To use multiprocessing for testing, Redis must be configured, or login will fail.