程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python automatic switching environment

編輯:Python

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 first 1 One way to use it is to add... To the terminal --base-url This command As shown below :
  • The first 2 One way to use it is in pytest.ini The configuration file is a way to configure base_url, Then automatically read url The data of , So you don't have to add --base-url This command-line parameter :

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 :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved