Testers deal with different environments every day , Such as online environment , Test environment , Pre launch environment, etc , So the code written as an Automation Tester , We should also have the ability to switch environments freely , So today Xiaobian will talk to you about , How can we python Test cases written in language can be freely switched to different environments to run ?
Python Writing test cases is inseparable from the unit test framework , Recently, the popular unit testing framework is pytest. There is also a reason for people's fire , Plugins are quite rich , Support multiple assertions , Exquisite Report , Cough , No more rainbow farting , Let's get back to business , Let's talk to him today pytest Several methods of framework to realize free environment switching .
1.
pytest-base-url
First of all ,pytest There is a plug-in called pytest-base-url, Is management base_url A very good plug-in , Documentation Links :https://pypi.org/project/pytest-base-url/
Download mode :
pip install pytest-base-url
The case is as follows :
The command line runs directly pytest that will do .
2.
hooks function
second ,pytest There is one hooks function , Sure Custom command line parameters , Generally in conftest.py To quote from .
Conftest.py Write in pytest_addoption This hooks function , You can customize the command line parameters ,base_url Only one url Address for delivery , So some projects not only need multi project requests url Address switching between different environments , Also need to mysql Of url Switch addresses differently , At this time, you can use custom commands , Define different command line parameters , So we're doing pytest You can transfer freely when you are free .
The code is as follows :
adopt pytest_addoption This hooks function , Set two command line parameters , One is --mysql-url, The other is --project-url,help The following is the explanation of these two command lines , Explain what these two command lines mean .
adopt fixture Function request Parameter to read the parameters passed in the command line , Assign to a variable , You can go to pytest Use casually in .
The project code is as follows :fixture Function call , Put... Directly fixture The name of can be passed to the test case as a parameter .
The command to run the test is :pytest -s --mysql-url 192.168.1.11
--project-url http://www.example.com, As shown in the figure below :
3.
yml and fixture
The third kind of , adopt yml Document and fixture Use it together
First, create a... Under the project directory conf.yml, Then manually configure the current running environment , The screenshot is as follows :
Then create a conftest.py, It defines two fixture function , One for reading conf.yml What's in it , The other is used to configure specific environment information , such as qa Environmental Science ,release Environmental Science , And then through conf.yml It's configured inside qa still release, To read project_url and mysql_url, The screenshot is as follows :
The project code is as follows : ( Or the previous test case )
Command line operation mode , Normal operation is enough , The screenshot is as follows :