pytest_03

pytest文档15-使用自定义标记mark

pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行。一个大项目自动化用例时,可以划分多个模块,
也可以使用标记功能,标明哪些是模块1用例,哪些是模块2的,运行代码时候指定mark名称运行就可以

mark标记

1.以下用例,标记test_send_http()为webtest

# content of test_server.py

import pytest

@pytest.mark.webtest
def test_send_http():
    pass # perform some webtest test for your app

只运行用webtest标记的测试,cmd运行的时候,加个-m 参数,指定参数值webtest

$ pytest -v -m webtest

如果不想执行标记webtest的用例,那就用"not webtest"

$ pytest -v -m "not webtest"

指定的函数节点id

如果想指定运行某个.py模块下,类里面的一个用例,如:TestClass里面test_method用例
每个test_开头(或_test结尾)的用例,函数(或方法)的名称就是用例的节点id,指定节点id运行用-v 参数

$ pytest -v test_server.py::TestClass::test_method

pycharm运行代码

if __name__ == "__main__":
    pytest.main(["-v", "test_server.py::TestClass::test_method"])

也能选择多个节点运行,多个节点中间空格隔开

$ pytest -v test_server.py::TestClass test_server.py::test_send_http

pycharm运行参考

if __name__ == "__main__":
    pytest.main(["-v", "test_server.py::TestClass", "test_server.py::test_send_http"])

您也可以运行所有的测试,根据用例名称排除掉某些用例:

$ pytest -k "not send_http" -v

-k 匹配用例名称

可以使用-k命令行选项指定在匹配用例名称的表达式

$ pytest -v -k http


也可以同时选择匹配 “http” 和“quick”

$ pytest -k "http or quick" -v

  • -v 用于显示每个测试函数的执行结果
  • -q 只显示整体测试结果
  • -s 用于显示测试函数中print()函数输出  (,未报错的情况下,打印print 信息;)

pytest文档17-fixture之autouse=True

写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传这个参数,会比较麻烦。
fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了

调用fixture三种方法

  • 1.函数或类里面方法直接传fixture的函数参数名称
  • 2.使用装饰器@pytest.mark.usefixtures()修饰
  • 3.autouse=True自动使用

方法一:先定义start功能,用例全部传start参数,调用该功能

# content of test_06.py
import time
import pytest

# ** 作者:上海-悠悠 QQ交流群:588402570**

@pytest.fixture(scope="function")
def start(request):
    print('
-----开始执行function----')


def test_a(start):
    print("-------用例a执行-------")




class Test_aaa():

    def test_01(self, start):
        print('-----------用例01--------------')

    def test_02(self, start):
        print('-----------用例02------------')

if __name__ == "__main__":
    pytest.main(["-s", "test_06.py"])

另一种方式

装饰器usefixtures

方法二:使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

@pytest.fixture(scope="function")
def start(request):
    print('
-----开始执行function----')


@pytest.mark.usefixtures("start")
def test_a():
    print("-------用例a执行-------")

@pytest.mark.usefixtures("start")
class Test_aaa():

    def test_01(self):
        print('-----------用例01--------------')

    def test_02(self):
        print('-----------用例02------------')

if __name__ == "__main__":
    pytest.main(["-s", "test_07.py"])
 

设置autouse=True

方法三、autouse设置为True,自动调用fixture功能

  • start设置scope为module级别,在当前.py用例模块只执行一次,autouse=True自动使用
  • open_home设置scope为function级别,每个用例前都调用一次,自动使用
  • import time
    import pytest
    
    
    @pytest.fixture(scope="module", autouse=True)
    def start(request):
        print('
    -----开始执行moule----')
        print('module      : %s' % request.module.__name__)
        print('----------启动浏览器---------')
        yield
        print("------------结束测试 end!-----------")
    
    
    
    @pytest.fixture(scope="function", autouse=True)
    def open_home(request):
        print("function:%s 
    --------回到首页--------" % request.function.__name__)
    
    
    def test_01():
        print('-----------用例01--------------')
    
    def test_02():
        print('-----------用例02------------')
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_08.py"])
  • 写在类里和上面类似,一样的
  • class Test_aaa():
        @pytest.fixture(scope="function", autouse=True)
        def open_home(self, request):
            print("function:%s 
    --------回到首页--------" % request.function.__name__)

 

原文地址:https://www.cnblogs.com/pythonwork/p/15100379.html