pytest教程之分组测试

本文介绍了pytest中的分组测试,包括如何使用分组测试、实现多分组以及为setup和teardown添加组别。通过实例代码展示了如何控制测试用例的执行,确保特定组别的测试用例能够正确运行。

分组测试用法

与java的TestNG一样,pytest同样有进行分组测试的方案,方法即使用@pytest.mark.组名的方式,譬如如下范例:

#-*- coding: utf-8 -*-
import pytest


class Test_simple():

    @pytest.mark.test
    def test_case1(self):
        print("testCase1")
        tof = True
        assert tof

    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
        assert tof

    def test_case3(self):
        print("testCase3")
        assert True

运行命令采用在pytest后添加-m "组名"的方式来运行,范例如下:

>pytest -m "test"

结果如下所示:

========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 1 deselected / 2 selected                                                                                                                                                                                                                           

testcase\Test_simple.py testCase1
.testCase2
F

如上所示只运行了test_case1test_case2,而test_case3则由于不属于组别test而没有运行;

多分组方法

那么一个用例是否可以从属于多个组呢?答案是可以的,代码范例如下:

#-*- coding: utf-8 -*-
import pytest


class Test_simple():

    @pytest.mark.test
    def test_case1(self):
        print("testCase1")
        tof = True
        assert tof

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
        assert tof

    def test_case3(self):
        print("testCase3")
        assert True

如上所示,test_case2同时从属于两个组,分别为testnormal,那么此时我只想运行normal的测试,怎么运行呢?如下:

E:\pyspace\testSimple>pytest -s -m "normal"

结果如下所示,只运行了一条用例,即test_case2,符合预期:

========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 2 deselected / 1 selected                                                                                                                                                                                                                           

testcase\Test_simple.py testCase2
F

为setup和teardown添加组别

在实际应用中,有时会出现这种情况,一个组的用例需要一个前置条件,而此时不同的组需要的前置条件不同,那么setup和teardown能否也设置组名,根据分组运行呢?答案也是可以的,这点也和TestNG是一样的,关于setup和teardown的用法请参考pytest的setup和teardown用法
范例如下:

#-*- coding: utf-8 -*-
import pytest


class Test_simple():

    @pytest.mark.test
    def test_case1(self):
        print("testCase1")
        tof = True
        assert tof

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
        assert tof

    def test_case3(self):
        print("testCase3")
        assert True

    @pytest.mark.test
    def setup_class(self):
        print("用于test组")

    @pytest.mark.normal
    def setup_class(self):
        print("用于normal组")

如上所示添加了两个setup_class,分别设置了两个组别,此时我想要只运行组名为normal的用例,预期应当是只跑组名为normal的那个setup_class,运行命令如下:

E:\pyspace\testSimple>pytest -s -m "normal"

结果如下所示,结果符合预期,确实只跑了名为normalsetup_class:

========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 2 deselected / 1 selected                                                                                                                                                                                                                           

testcase\Test_simple.py 用于normal组
testCase2
F

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值