示例代码:
# -*- coding: utf-8 -*-
import unittest
import pytest
from mudules.mudules import skip1, skipif1, run_these_please
class TestDemo(unittest.TestCase):
# @skipif1 # pytest.mark.skipif(1 + 1 == 2, reason='跳过') 在mudules.mudules中定义的
@skip1 # skip1 = pytest.mark.skip('跳过') 在mudules.mudules中定义的
@pytest.mark.run_these_please
def test_test01(self):
print('进行testcase01')
# self.assertEqual(1, 11, "判断相等")
assert 1 == 11
assert 1 <= 2
assert 'h' in 'thes'
# 跳过某条用例
# @unittest.skip('跳过')
@pytest.mark.skip('跳过')
def test_test02(self):
print('进行testcase02')
self.assertIn('h1', 'this', '判断是否在里面')
# 跳过用例当if条件成立
# @unittest.skipIf(1 + 1 == 2, 'skipif跳过')
@pytest.mark.skipif(1 + 1 == 2, reason='skipif跳过')
def test_test03(self):
print('进行testcase03')
self.assertIn('h1', 'this', '判断是否在里面')
@pytest.mark.run_these_please
# @run_these_please #run_these_please = pytest.mark.run_these_please 在mudules.mudules中定义的
def test_test04(self):
print('进行testcase04')
# self.assertIn(h, 'this', '语法错误,str未用引号引起来')
assert 1 == 11
assert 1 <= 2
assert 'h' in 'thes'
# @pytest.mark.run_these_please
@pytest.mark.xfail(resason='sasdasd')
def test_test05(self):
print('进行testcase05')
# self.assertIn(h, 'this', '语法错误,str未用引号引起来')
assert 1 == 11
assert 1 <= 2
assert 3 >= 2
assert 'h' in 'thes'
assert 'hh' not in 'h1y'
跳过测试用例
使用如下所示进行标记
@pytest.mark.skip()
@pytest.mark.skipif(condition, reason=None)
运行示例代码,运行结果如下,可以看出被标记为skip 或skipif的用例跳过未运行
标记测试用例
打标签:@pytest.mark.tagname
运行示例代码中@pytest.mark.run_these_please标记的测试用例
pytest -v .\test_case_demo_pytest.py -m run_these_please
标记测试函数为预期失败:
使用@pytest.mark.xfail(condition=None, reason=None, raises=None, run=True, strict=False)标记函数,如下代码片段所示:
import pytest
class TestDemo(unittest.TestCase):
@pytest.mark.xfail(resason='预期失败并且执行失败')
def test_test05(self):
print('进行testcase05')
# self.assertIn(h, 'this', '语法错误,str未用引号引起来')
assert 1 == 11
assert 1 <= 2
assert 3 >= 2
assert 'h' in 'thes'
assert 'hh' not in 'h1y'
@pytest.mark.xfail(resason='预期失败但是执行成功')
def test_test06(self):
print('进行testcase05')
# self.assertIn(h, 'this', '语法错误,str未用引号引起来')
assert 1 == 1
assert 1 <= 2
assert 3 >= 2
assert 'h' in 'thes'
assert 'hh' not in 'h1y'
运行命令
pytest -v .\test_case_demo_pytest.py -k 'test_test05 or test_test06'
运行结果: