一.mark
pytest提供了标记机制,允许你使用marker对测试函数做标记,一个测试函数可以有多个marker,一个marker也可以用来标记多个测试函数
1.背景:
当我们需要进行冒烟测试,不可能把所有的用例都跑一遍,我们可以挑选一些重要的用例进行冒烟测试,为了把选定的测试加入冒烟测试,可以对他们添加@pytest.mark.smoke装饰器。
案例1:只执行smoke类型的测试用例
test_menus1.py
# 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”:必须使用双引号
二.跳过测试skip
要跳过某个测试,只需要简单的在测试函数上方添加@pytest.mark.skip()装饰器即可
案例1: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
案例2:如果在整个类中添加@pytest.mark.skip():表示整个类中的测试用例都将跳过
#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
我们可以给要跳过的测试添加理由和条件,这时应当使用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
四.运行测试子集
运行测试有很多方式,不但可以选择运行某个测试目录、文件、类中的测试,还可以选择运行某一个测试用例
目录结构
1.运行单个目录
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
2.运行单个测试文件
运行单个文件里的全部测试,以路径名加文件名作为pytest参数即可
执行命令
D:\project_development\api_pytest>pytest testcases/menus/test_menus1.py
3.运行单个测试函数
运行单个测试函数,只需要在文件名后添加::符号和函数名
执行命令
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>])
4.运行单个测试类
与运行单个函数类似,只需要在文件名后面加上::符号和类名
执行命令
D:\project_development\api_pytest>pytest testcases\menus\test_menus1.py::TestMenus
5.用测试名划分测试集合
-k选项允许用一个表达式指定需要运行的测试,该表达式可以匹配测试名(或者子串)。表达式中可以包含and,or,not
案例:执行用例中包含’2’的测试用例
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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/73942.html