pytest提供了标记机制,允许你使用marker对测试函数做标记,一个测试函数可以有多个marker,一个marker也可以用来标记多个测试函数
当我们需要进行冒烟测试,不可能把所有的用例都跑一遍,我们可以挑选一些重要的用例进行冒烟测试,为了把选定的测试加入冒烟测试,可以对他们添加@pytest.mark.smoke装饰器。
# encoding=utf-8
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@pytest.mark.smoke
def test_menu1(self):
logging.info('执行测试用例1')
assert 2 == 2
#@pytest.mark.repeat(2)
@pytest.mark.smoke
def test_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
def test_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '__main__':
pytest.main()
test_work1.py
#encoding=utf-8
import pytest
class TestWork:
@pytest.mark.smoke
def test_h(self,home_url):
print("用例:%s" % home_url)
def test_work1(self):
assert 1 == 1
def test_work2(self):
assert 1 == 1
if __name__ == '__main__':
pytest.main()
执行命令行:
pytest
我们可以看到,
测试模块menus—>test_menus1.py文件—>类上加了装饰器,也就是说需要执行3条用例;
测试模块menus—>test_menus1.py文件—>test_h加了装饰器,说明执行1条用例
总需要执行4条用例
-m参数执行一下,这样我们标记的smoke的用例就被执行了,其他的没有被执行
-m后面也可以使用表达式,可以在标记之间添加and,or,not关键字
执行命令:表示执行没有标记smoke的测试用例
pytest -m "not smoke"
特别注意
pytest -m “not smoke”:必须使用双引号
要跳过某个测试,只需要简单的在测试函数上方添加@pytest.mark.skip()装饰器即可
test_menus.py
# encoding=utf-8
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@pytest.mark.skip()
@pytest.mark.smoke
def test_menu1(self):
logging.info('执行测试用例1')
assert 2 == 2
#@pytest.mark.repeat(2)
@pytest.mark.smoke
def test_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
def test_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '__main__':
pytest.main()
test_work1.py
#encoding=utf-8
import pytest
class TestWork:
@pytest.mark.smoke
def test_h(self,home_url):
print("用例:%s" % home_url)
@pytest.mark.skip()
def test_work1(self):
assert 1 == 1
def test_work2(self):
assert 1 == 1
if __name__ == '__main__':
pytest.main()
执行测试命令
pytest
#encoding=utf-8
import pytest
@pytest.mark.skip()
class TestWork:
@pytest.mark.smoke
def test_h(self,home_url):
print("用例:%s" % home_url)
@pytest.mark.skip()
def test_work1(self):
assert 1 == 1
def test_work2(self):
assert 1 == 1
if __name__ == '__main__':
pytest.main()
命令行执行:
pytest
我们可以给要跳过的测试添加理由和条件,这时应当使用skipif来替代skip
前面的是个条件,可以是python的表达式,表达式的值为True时跳过,为false时执行。后面的是原因,在来执行一次
test_menus1.py
# encoding=utf-8
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@pytest.mark.skipif(24<8,reason='版本不匹配')
@pytest.mark.smoke
def test_menu1(self):
logging.info('执行测试用例1')
assert 2 == 2
#@pytest.mark.repeat(2)
@pytest.mark.smoke
def test_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
def test_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '__main__':
pytest.main()
test_work1.py
#encoding=utf-8
import pytest
#@pytest.mark.skip()
class TestWork:
@pytest.mark.smoke
def test_h(self,home_url):
print("用例:%s" % home_url)
@pytest.mark.skipif(8<24,reason='版本不匹配')
def test_work1(self):
assert 1 == 1
def test_work2(self):
assert 1 == 1
if __name__ == '__main__':
pytest.main()
执行命令
-rs:控制台查看跳过信息
pytest -rs
运行测试有很多方式,不但可以选择运行某个测试目录、文件、类中的测试,还可以选择运行某一个测试用例
test_menus1.py
# encoding=utf-8
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@pytest.mark.skipif(24<8,reason='版本不匹配')
@pytest.mark.smoke
def test_menu1(self):
logging.info('执行测试用例1')
assert 2 == 2
#@pytest.mark.repeat(2)
@pytest.mark.smoke
def test_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
def test_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '__main__':
pytest.main()
test_work1.py
#encoding=utf-8
import pytest
#@pytest.mark.skip()
class TestWork:
@pytest.mark.smoke
def test_h(self,home_url):
print("用例:%s" % home_url)
@pytest.mark.skipif(8<24,reason='版本不匹配')
def test_work1(self):
assert 1 == 1
def test_work2(self):
assert 1 == 1
if __name__ == '__main__':
pytest.main()
执行命令
D:\project_development\api_pytest>pytest testcases/menus
collecting ... ----这是前置方法----
testcases\menus\test_menus1.py::TestMenus.test_menu1 ✓ 33% ███▍
testcases\menus\test_menus1.py::TestMenus.test_menu2 ✓ 67% ██████▋
----这是后置方法----
testcases\menus\test_menus1.py::TestMenus.test_menu3 ✓ 100% ██████████
Results (0.10s):
3 passed
运行单个文件里的全部测试,以路径名加文件名作为pytest参数即可
执行命令
D:\project_development\api_pytest>pytest testcases/menus/test_menus1.py
运行单个测试函数,只需要在文件名后添加::符号和函数名
执行命令
D:\project_development\api_pytest>pytest testcases\menus\test_menus1.py::TestMenus::test_menu1
ERROR: not found: D:\project_development\api_pytest\testcases\menus\test_menus1.py::test_menu1
(no name 'D:\\project_development\\api_pytest\\testcases\\menus\\test_menus1.py::test_menu1' in any of [<Module test_menus1.py>])
与运行单个函数类似,只需要在文件名后面加上::符号和类名
执行命令
D:\project_development\api_pytest>pytest testcases\menus\test_menus1.py::TestMenus
-k选项允许用一个表达式指定需要运行的测试,该表达式可以匹配测试名(或者子串)。表达式中可以包含and,or,not
test_menus1.py
# encoding=utf-8
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@pytest.mark.skipif(24<8,reason='版本不匹配')
@pytest.mark.smoke
def test_menu1(self):
logging.info('执行测试用例1')
assert 2 == 2
#@pytest.mark.repeat(2)
@pytest.mark.smoke
def test_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
def test_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '__main__':
pytest.main()
test_work1.py
#encoding=utf-8
import pytest
#@pytest.mark.skip()
class TestWork:
@pytest.mark.smoke
def test_h(self,home_url):
print("用例:%s" % home_url)
@pytest.mark.skipif(8<24,reason='版本不匹配')
def test_work1(self):
assert 1 == 1
def test_work2(self):
assert 1 == 1
if __name__ == '__main__':
pytest.main()
执行命令
D:\project_development\api_pytest>pytest -k 2