What is the tox
Taichichuan contains taichichuan testing in Python. Taichichuan contains taichichuan testing in Python. Taichichuan contains Taichichuan testing in Python.
Tox is a command-line tool for managing test virtual environments. It supports a piece-isolated Python environment where different versions of the Python interpreter can be installed and various dependent libraries for the project can be used for automated testing, packaging, and continuous integration, according to the documentation.
What can TOX do
- Create a test virtual environment
- Run static code analysis and test tools
- Automated build pack
- Run tests against the tox-built software package
- Check that the package can be installed successfully in different Python versions/interpreters
- Unified Continuous integration (CI) and command-line based testing
How to Configure TOX
Install the tox
Using the PIP Install tox installation, execute tox -e envname on the command line to run the specified test environment
Tox configuration
Tox behavior can be controlled either from the command line or through configuration files. The following three types of configuration files are supported
-
pyproject.toml
-
tox.ini
-
setup.cfg
# tox (https://tox.readthedocs.io/) is a tool for running tests # in multiple virtualenvs. This configuration file will run the # tests suite on all supported python versions. To use it, PIP install tox # and then run "tox" from this directory. [tox] envlist = py36 Skipsdist = True # indexserver = default = http://mirrors.aliyun.com/pypi/simple/ [testenv] deps = pytest records pymysql jinja2 requests objectpath arrow pytest-html redis install_command = pip install --trusted-host mirrors.aliyun.com {opts} {packages} [testenv:dev] setenv = env = dev ; Tell tox to run PyTest Commands = PyTest --junitxml=junit-{envName}.xml; Run only ad-related test cases [testenv:t_a] setenv = env = dev commands = Pytest -v tests/ AD --junitxml=junit-{envName}.xml; Only run test cases related to test environment APP; Run only app-related test cases [testenv:t_i] setenv = env = dev commands = Pytest -v tests/ivwen --junitxml=junit-{envName}.xml [testenv:t1_i] setenv = env = t1 commands = pytest -v tests/ivwen --junitxml=junit-{envname}.xml [testenv:pro] setenv = env = pro ; Config reads the specified file from the environment variable according to the test or line identifier written by command line. Or, in the form of plug-ins, files that can be configured for each environment, Command = pytest [testenv:smoke] [Pytest] markers = smoke get addopts = -rsxx -l -- TB =short --strict xfail_strict = true minversion = 3.0 norecursedirs =.* venv SRC *. Egg dist build testPaths = tests python_classes = *Test Test* *Suit junit_family=xunit1Copy the code
The configuration is explained as follows:
- The [tox] node configures tox
Envlist Specifies a list of environments, separated by commas (py36,py37)
Skipsdist specifies that tox skips packaging during runtime. Since there is no packaging requirement for the current project, this is set to true. This is related to the design of the automated test framework.
Indexserver specifies the PIP installation source
- The [testenv] node is used to configure the test environment. This is the configuration of the root test environment, and can also be used to configure different test environments
Deps specifies the third-party packages on which the project’s Python depends
Install_command defines PIP installation command parameters
- [testenv:dev] This object defines the test environment and inherits the root environment configuration
Setenv sets environment variables that can be read in a project to determine which environment configuration to run, such as tox-e dev, which means test cases are run in the test environment and tox-e Prod is run in the production environment
Commands specifies how PyTest should run, and node configurations in other environments are similar.
- Pytest can be configured by the [PyTest] node
- Addopts specifies command line arguments for PyTest
- Xfail_strict Sets the expected failed case. If it passes, it is marked as failed
- Minversion Specifies the minimum version of TOX
- Norecursedirs specifies which directories do not recursively look up test cases
- Testpaths specifies the search directory for test cases
- Python_classes specifies the search rules for test cases
Of course, the above configuration is only a part of TOX, there are many more, pay attention to the official documentation
Tox Project Practice
Let’s build an automated testing framework with Tox and PyTest
Project structures,
-
Create a new api-auto-test folder, add an tox. Ini file to the folder and enter the configuration above
-
Create a new SRC and tests directory, respectively. SRC is used to store the encapsulated common content, and tests is used to store the test cases
-
The SRC directory contains the following contents
AD and BIZ encapsulate different services, including interface invocation and database-related operations
Common is the common part of each service module, including request sending, database link basic operation encapsulation, configuration, etc., mainly see the content in config:
class Config:
Common configuration
class DevConfig(Config):
"Test environment Configuration"
class ProdConfig(Config):
Production Environment Configuration
# Environment switch
_MAPPING = {
'dev': DevConfig,
't1': T1Config,
'pro': ProConfig,
}
# Here, the tox environment variable is used to determine which environment to use, so as to realize the switch between different environments
config = _MAPPING.get(os.getenv("env"), DevConfig)
Copy the code
-
Run the test case
tox -e dev
The above is the execution process and test results. The test report will be generated in junit. XML format, of course, you can also use Pytest-html or other test report, which is very convenient.
You are welcome to check out my blog, where there is more about the actual test content oh!!