pytest教程之分組測試

分組測試用法

與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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章