测试案例要和源码分开,单独放在一个tests文件夹里
tests文件夹位于项目根目录
一般测试用例的目录这样
conftest包含hook函数和fixture
pytest.ini,保存pytest在该项目下的配置
断言有两种:第一种,在assert 后面添加任何表达式
assert
第二种:异常类型的断言,with pytest.raise()
def test_div_one():
with pytest.raises(ZeroDivisionError):
result=my_div(1,0)
更进一步,如果想断言异常抛出的信息,可以使用value.args[0]来获取到异常信息
def test_div_two():
with pytest.raises(ZeroDivisionError) as exceptInfo:
my_div(1,0)
excepted_msg=exceptInfo.value.args[0]
assert excepted_msg=='division by zero'
管理测试用例
(1)通过使用marker(标记)对测试用例进行分组
比如选择一些测试用例作为冒烟测试,可以使用装饰器pytest.mark.smoke来标记这些测试用例
运行的时候,通过-m选项来指定运行那些标记
pytest -m “smoke”
表示运行标记为smoke的测试用例
-m后面也可以使用表达式,可以在标记之前添加 and or not等
比如 pytest -m “smoke and get” 运行标记为smoke和get的测试用例
pytest内置了一些标记,skip,skipif xfail
skip和skipif允许跳过不希望运行的测试用例
只是简单的跳过某个测试用例,使用pytest.mark.skip(reason=" xxxx")
如果要根据某个条件来判断是否跳过用例,使用pytest.mark.skipif(condition,reason), condition是条件,当条件是true时,跳过该测试用例,reason是原因
如果使用多组数据来测试同一个测试用例,可以使用@pytest.mark.parametrize(argnames,argvalues)装饰器达到批量传送参数的目的
第一个参数是str,List[str],Tuple[str]
第二个参数是一个值列表
@pytest.mark.parametrize("a",[1,5,7])
def test_parmetrized(a):
print(a+1)
@pytest.mark.parametrize(["a","b"],[(1,2),(3,4)])
def test_parametrized_one(a,b):
print(a+b)
运行结果:
C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py" ============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe cachedir: .pytest_cache rootdir: E:JenkinsLearnMy-pytesttest, configfile: pytest.ini plugins: cov-3.0.0 collecting ... collected 2 items test_my_add.py::test_parametrized_one[1-2] 3 PASSED test_my_add.py::test_parametrized_one[3-4] 7 PASSED
@pytest.mark.parametrize(("a","b","c"),[("one","two","three")])
def test_paramtrized_two(a,b,c):
print(a+b+c)
运行结果:
C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py" ============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe cachedir: .pytest_cache rootdir: E:JenkinsLearnMy-pytesttest, configfile: pytest.ini plugins: cov-3.0.0 collecting ... collected 1 item test_my_add.py::test_paramtrized_two[one-two-three] onetwothree PASSED
@pytest.mark.parametrize("a,b,c",[(1,2,3),(4,5,6)])
def test_parametrized_three(a,b,c):
print(a+b+c)
运行结果:
C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe "E:/JenkinsLearn/My-pytest/test/api case/test_my_add.py" ============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe cachedir: .pytest_cache rootdir: E:JenkinsLearnMy-pytesttest, configfile: pytest.ini plugins: cov-3.0.0 collecting ... collected 2 items test_my_add.py::test_parametrized_three[1-2-3] 6 PASSED test_my_add.py::test_parametrized_three[4-5-6] 15 PASSED运行测试子集
运行单个目录:pytest tests/func
运行单个测试文件/模块 pytest tests/func/test_add.py
运行单个测试函数 pytest tests/func/test_add.py::test_one
运行单个测试类 pytest tests/func/test_add.py::TestAdd
运行测试类中的单个测试方法 pytest tests/func/test_add.py::TestAdd::test_one



