分组测试用法
与java的TestNG一样,pytest同样有进行分组测试的方案,方法即使用@pytest.mark.组名的方式,譬如如下范例:
#-*- coding: utf-8 -*-
import pytest
class Test_simple():
@pytest.mark.test
def test_case1(self):
print("testCase1")
tof = True
assert tof
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
assert tof
def test_case3(self):
print("testCase3")
assert True
运行命令采用在pytest后添加-m "组名"的方式来运行,范例如下:
>pytest -m "test"
结果如下所示:
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 1 deselected / 2 selected
testcase\Test_simple.py testCase1
.testCase2
F
如上所示只运行了test_case1和test_case2,而test_case3则由于不属于组别test而没有运行;
多分组方法
那么一个用例是否可以从属于多个组呢?答案是可以的,代码范例如下:
#-*- coding: utf-8 -*-
import pytest
class Test_simple():
@pytest.mark.test
def test_case1(self):
print("testCase1")
tof = True
assert tof
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
assert tof
def test_case3(self):
print("testCase3")
assert True
如上所示,test_case2同时从属于两个组,分别为test和normal,那么此时我只想运行normal的测试,怎么运行呢?如下:
E:\pyspace\testSimple>pytest -s -m "normal"
结果如下所示,只运行了一条用例,即test_case2,符合预期:
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 2 deselected / 1 selected
testcase\Test_simple.py testCase2
F
为setup和teardown添加组别
在实际应用中,有时会出现这种情况,一个组的用例需要一个前置条件,而此时不同的组需要的前置条件不同,那么setup和teardown能否也设置组名,根据分组运行呢?答案也是可以的,这点也和TestNG是一样的,关于setup和teardown的用法请参考pytest的setup和teardown用法;
范例如下:
#-*- coding: utf-8 -*-
import pytest
class Test_simple():
@pytest.mark.test
def test_case1(self):
print("testCase1")
tof = True
assert tof
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
assert tof
def test_case3(self):
print("testCase3")
assert True
@pytest.mark.test
def setup_class(self):
print("用于test组")
@pytest.mark.normal
def setup_class(self):
print("用于normal组")
如上所示添加了两个setup_class,分别设置了两个组别,此时我想要只运行组名为normal的用例,预期应当是只跑组名为normal的那个setup_class,运行命令如下:
E:\pyspace\testSimple>pytest -s -m "normal"
结果如下所示,结果符合预期,确实只跑了名为normal的setup_class:
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 2 deselected / 1 selected
testcase\Test_simple.py 用于normal组
testCase2
F

本文介绍了pytest中的分组测试,包括如何使用分组测试、实现多分组以及为setup和teardown添加组别。通过实例代码展示了如何控制测试用例的执行,确保特定组别的测试用例能够正确运行。
1380

被折叠的 条评论
为什么被折叠?



