api的编写:完整的api测试用例中,要考虑到每个测试点的初始化,测试步骤,测试断言还有清理的操作
unittest和pytest框架之间的区别:
1,unittest需要继承unitttest.TestCase类,pytest不需要继承,可以是函数也可以是类。
2,unittest参数化需要依赖第三方库比如说ddt,pytest参数化直接用内部的parametrize
3,unittest测试报告是HTMLTestRunner,pytest是Pytest-html或allure
4,unittest没有插件,pytest有丰富的插件如:pytest-cov
5,unittest不支持失败重试,pytest支持失败重试
Pytest使用规范:
测试用例的结果信息:
import pytest
def test_passing():
assert (1,2,3)==(1,2,3)
def test_failing():
assert 1==2
@pytest.mark.skip() # 跳过
def test_skip():
assert 1==1
@pytest.mark.xfail() # 预期失败
def test_xfail():
assert 1==2
@pytest.mark.xpass() # 预期成功
def test_xpass():
assert 1==1
pytest测试用例的执行顺序是按从上往下的顺序执行的
pytest的执行方式:
def test_login_001():
assert 1==1
'''tests包名 test_login.py:文件名'''
class TestBaidu(object):
def test_baidu_001(self):
pass
def test_baidu_002(self):
pass
package级别:python -m pytest -v tests
model级别:python -m pytest -v tests/test_login.py::test_login_001
class级别:python -m pytest -v tests/test_login.py::TestBaidu
pytest执行命令
按分类执行测试用例
python3 -m pytest -v -k "profile or register"
添加分组:@pytest.mark.login(组名) 分组执行python -m pytest -v -m "login"
执行失败立刻停止:python3 -m pytest -v -x test_login.py
指定失败次数:--maxfail:pytest -v -x --maxfail=2 -m login
定位错误:--lf pytest --lf -m login
忽略执行: -rs
pytest -rs
import pytest
@pytest.mark.login
def test_login_001():
pass
@pytest.mark.skip(reason='忽略执行该测试点')
def test_login_002():
pass
Pytest与WebDriver
import pytest
def test_baidu_title(selenium):
selenium.get('https://www.baidu.com/')
assert selenium.title == '百度一下,你就知道'
def test_baidu_url(selenium):
selenium.get('http://www.baidu.com/')
assert selenium.current_url == 'https://www.baidu.com/'
'''执行命令:pytest -v pytest-selenium.py --driver Firefox'''