1.参数化结合allure.title()生成不同标题报告
1.参数化parametrize
import os
import pytest
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
return {"code":0,"msg":"success!"}
test_datas = [
({"username":"zz","password":"111111"},"success!"),
({"username":"xz","password":"222222"},"failed!"),
({"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
result = login(test_input["username"],test_input["password"])
assert result["msg"] == expected
if __name__ == '__main__':
pytest.main(['test_a_01.py','--alluredir','./result','--clean-alluredir'])
os.system('allure generate ./result -o ./report_allure/ --clean')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
2.param+ids参数
import os
import pytest
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
return {"code":0,"msg":"success!"}
test_datas = [
({"username":"zz","password":"111111"},"success!"),
({"username":"xz","password":"222222"},"failed!"),
({"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas,
ids=["输入正确的账号A,密码,登录",
"输入错误的账号B,密码,登录",
"输入正确的账号A,密码,登录"
]
)
def test_login(test_input,expected):
"""测试登录用例"""
result = login(test_input["username"],test_input["password"])
assert result["msg"] == expected
if __name__ == '__main__':
pytest.main(['test_a_ids_02.py','--alluredir','./result2','--clean-alluredir'])
os.system('allure generate ./result2 -o ./report_allure2/ --clean')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
3.allure.title描述用例
import os
import allure
import pytest
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
return {"code":0,"msg":"success!"}
test_datas = [
({"username":"zz","password":"111111"},"success!"),
({"username":"xz","password":"222222"},"failed!"),
({"username":"qs","password":"333333"},"success!")
]
@allure.title("用例描述,测试输入:{test_input}")
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
result = login(test_input["username"],test_input["password"])
assert result["msg"] == expected
if __name__ == '__main__':
pytest.main(['test_a_title_03.py','--alluredir','./result3','--clean-alluredir'])
os.system('allure generate ./result3 -o ./report_allure3/ --clean')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
4.优化用例title
import os
import allure
import pytest
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
return {"code":0,"msg":"success!"}
test_datas = [
({"username":"zz","password":"111111"},"success!","输入正确的账号,密码登录"),
({"username":"xz","password":"222222"},"failed!","输入错误的账号,密码登录"),
({"username":"qs","password":"333333"},"success!","输入正确的账号,密码登录")
]
@allure.title("{title}{test_input}")
@pytest.mark.parametrize("test_input,expected,title",test_datas)
def test_login(test_input,expected,title):
"""测试登录用例"""
result = login(test_input["username"],test_input["password"])
assert result["msg"] == expected
if __name__ == '__main__':
pytest.main(['test_a_title_new_04.py','--alluredir','./result4','--clean-alluredir'])
os.system('allure generate ./result4 -o ./report_allure4/ --clean')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
2.allure报告清空上一次运行的记录
import os
import pytest
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
return {"code":0,"msg":"success!"}
test_datas = [
({"username":"zz","password":"111111"},"success!"),
({"username":"xz","password":"222222"},"failed!"),
({"username":"qs","password":"333333"},"success!")
]
def test_case02():
assert 1 == 1
if __name__ == '__main__':
pytest.main(['test_a_05.py','--alluredir','./result','--clean-alluredir'])
os.system('allure generate ./result -o ./report_allure/ --clean')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
3.allure动态生成用例标题
import os
import allure
import pytest
desc = "请求URL:{}
" \
"请求类型L:{}
" \
"期望结果:{}
" \
"实际结果:{}
" \
.format("http://www.baidu.com","post","200","404")
@allure.description("简单描述")
def test_dynamic_desc():
assert 1 == 1
allure.dynamic.description(desc)
@allure.title("原始标题")
def test_dynamic_title():
assert 2 + 2 == 4
allure.dynamic.title("当断言成功时,用例标题会动态更新")
@allure.title("参数化用例标题:添加{param1}和{param2}")
@pytest.mark.parametrize('param1,param2,expected',[(2,2,4),(1,2,5)])
def test_with_param_title(param1,param2,expected):
assert param1 + param2 == expected
allure.dynamic.title("变更标题")
if __name__ == '__main__':
pytest.main(['-s','test_case_06.py','--alluredir','./result','--clean-alluredir'])
os.system('allure generate ./result -o ./report_allure/ --clean')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
4.allure生成环境配置
在result的目录下添加一个environment.properties文件
systemVersion=win10
pythonVersion=3.6.0
allureVersion=2.13.0
baseUrl=http://192.168.1.x:8080
projectName=test
author=zz
email=123123123@qq.com
freind=Mr.ma
idl=wangjie
在allure的result目录下添加一个environment.xml
<environment>
<parameter>
<key>Browser</key>
<value>Chrome</value>
</parameter>
<parameter>
<key>Browser.Version</key>
<value>63.0</value>
</parameter>
<parameter>
<key>Stand</key>
<value>Production</value>
</parameter>
</environment>
import os
import pytest
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
return {"code":0,"msg":"success!"}
test_datas = [
({"username":"zz","password":"111111"},"success!"),
({"username":"xz","password":"222222"},"failed!"),
({"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
result = login(test_input["username"],test_input["password"])
assert result["msg"] == expected
if __name__ == '__main__':
pytest.main(['test_a_07.py','--alluredir','./result'])
os.system('allure generate ./result -o ./report_allure/ --clean')
import os
import pytest
def login(username,password):
"""登录"""
print("输入账户: %s"% username)
print("输入密码: %s"% password)
return {"code":0,"msg":"success!"}
test_datas = [
({"username":"zz","password":"111111"},"success!"),
({"username":"xz","password":"222222"},"failed!"),
({"username":"qs","password":"333333"},"success!")
]
@pytest.mark.parametrize("test_input,expected",test_datas)
def test_login(test_input,expected):
"""测试登录用例"""
result = login(test_input["username"],test_input["password"])
assert result["msg"] == expected
if __name__ == '__main__':
pytest.main(['test_a_08.py','--alluredir','./result','--clean-alluredir'])
os.system('copy environment.properties result\\environment.properties')
os.system('allure generate ./result -o ./report_allure/ --clean')

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
5.Allure报告添加失败截图
conftest.py
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope = "session")
def browser():
global driver
driver = webdriver.Chrome()
yield driver
driver.quit()
print("test end!!")
"""
装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用:
a.可以获取测试用例的信息,比如用例函数的描述
b.可以获取测试用例的结果,yield,返回一个result对象
"""
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
from time import sleep
def test_baidu_case01(browser):
driver = browser
driver.get("http://www.baidu.com")
sleep(2)
driver.find_element('id','kw').send_keys('狗狗币')
sleep(2)
driver.find_element('id','su').click()
sleep(2)
assert driver.title == "11狗狗币_百度搜索"
import os
import pytest
def run():
pytest.main(['-v', '--alluredir', './result', '--clean-alluredir'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74