pytest会进行自动查找默认查找
自定义查找规则 在运行的根目录下,创建pytest.ini文件
[pytest]
python_files =
test_*.py
check_*.py
example_*.py
python_functions = *_test
python_classes = *Suite
以上配置文件表示,pytest查找模块名为test_,check_,example_开头的模块,函数名为_test结尾的文件,Suite结尾的类
1.指定名称或目录进行测试
2.通过节点id进行测试
节点id的组成:
例:
pytest test_add.py::TestDemo::test_add_1
3.-k关键字匹配
pytest -k "test_add and not test_add_1"
运行test_add 不运行 test_add_1
4.-m标记用例 使用mark标记测试用例
标记测试用例步骤
标记测试用例方法 1.类标记 方法一:使用 @pytest.mark.名称 进行标记
@pytest.mark.error
class TestLogin(unittest.TestCase):
def test_login_1(self):
self.assertEqual(1,1)
def test_login_2(self):
self.assertEqual(1,2)
方法二:使用类属性 pytestmark = [pytest.mark.名称,pytest.mark.名称]
class TestLogin(unittest.TestCase):
pytestmark = [pytest.mark.login,pytest.mark.error]
def test_login_1(self):
self.assertEqual(1,1)
def test_login_2(self):
self.assertEqual(1,2)
2.方法标记 使用标签 @pytest.mark.名称
class TestDemo(unittest.TestCase):
@pytest.mark.success
def test_add_1(self):
self.assertEqual(1,1)
@pytest.mark.login
def test_add_2(self):
self.assertEqual(1,2)
3.跳过标记 @pytest.mark.skip(“跳过理由”) @pytest.mark.skipif(“条件”) 例如:满足系统是windows时跳过
@pytest.mark.skipif(sys.platform=="win32")
pytest的执行顺序安装方法写的前后顺序决定 unnittest的执行顺序由函数名的ASCII码决定