pytest框架实现UI automation由几个部分组成:
1. feature文件(写case的地方,由Given,When,Then组成),形如:
Feature: Login Feature
@regression @smoke ## tag, 跑自动化的时候可以根据你的tag来跑
Scenario: Search in CSDN web ## case步骤名称,如果使用allure,会显示在报告上
Given Login CSDN page
When Search "python"
Then Article related to "python" will be searched out
2. step.py文件(必须有step定义文件,feature文件才能调用step),形如:
from xxx import Login ## 如果import的对象是class需要定义
login: Login = None
@pytest.fixture(scope="function")
def before_login():
global login
login = Login()
@Given(parse('Login {web} page'))
def login_csdn_page(before_login, web):
xxxxxx
3. 然后就是封装的一些方法,只要是py文件就可以
4. test_all文件(名称自定义例如是regression还是sanity smoke,需要将所有需要用到的feature文件import到这个文件里,否则运行时报错找不到定义),形如:
from pytest_bdd import scenarios, scenario
from xxx import *
from xxx import *
scenarios("./features/") ## 所有feature文件所在的文件夹
5. TestENVConf.YAML(环境配置文件,需要用到的credentials或者其他需要额外配置的),形如:
TestENVConf:
DEV:
auth_url: https//test/dev.com
QA:
auth_url: https//test/qa.com
6. launch.json(自动化配置),形如:
{ "version": "0.2.0", "configurations": [ { "name": "PyTest", "type": "python", "request": "launch", "stopOnEntry": false, "module": "pytest", "args": [ "-vs", ".\\xxx\\test_all.py", ## test文件 "-m", "regression and smoke", ## tag "--config_file", "TestENVConf", ## env文件 "--config_node", "DEV", ## env "--alluredir", ".\\report\\json\\" ], "cwd": "${workspaceFolder}/tests/", "console": "integratedTerminal", "envFile": "${workspaceFolder}/.env", "justMyCode": false } ] }
需用到的拓展:pytest_bdd (文件高亮,跳转bdd step文件)
jdk下载
allure-pytest下载
allure generate ./report/result -o ./report/html --clean