2014-10-16 77 views
26

我使用硒進行端到端測試,我無法得到如何使用setup_classteardown_class方法。我如何正確設置並拆除測試中的pytest類?

我需要在setup_class方法中設置瀏覽器,然後執行一系列定義爲類方法的測試,最後在teardown_clas的方法中退出瀏覽器。

但從邏輯上來看,這似乎是一個不好的解決方案,因爲事實上,我的測試不會與類,但與對象。我通過每一個測試方法裏面self PARAM,所以我可以訪問對象的增值經銷商:

class TestClass: 

    def setup_class(cls): 
     pass 

    def test_buttons(self, data): 
     # self.$attribute can be used, but not cls.$attribute? 
     pass 

    def test_buttons2(self, data): 
     # self.$attribute can be used, but not cls.$attribute? 
     pass 

    def teardown_class(cls): 
     pass 

它甚至似乎並沒有被正確的類。應該爲每個創建創建瀏覽器實例對象分開,對不對?

那麼,我需要用__init____del__方法代替setup_classteardown_class

+0

+1這也一直困擾着我。爲什麼沒有setup_instance(self)和teardown_instance(self)方法? (或者安裝/ teardown_object?)這不是更正確的OO嗎? – cbare 2017-11-14 18:56:03

回答

25

當你寫「定義爲類的方法測試」,你真的是類方法其收到實例爲(方法它接收其類作爲第一個參數),或者只是普通的方法(methods第一個參數)?

由於您的示例使用self的測試方法我假設是後者,那麼你只需要使用setup_method代替:

class Test: 

    def setup_method(self, test_method): 
     # configure self.attribute 

    def teardown_method(self, test_method): 
     # tear down self.attribute 

    def test_buttons(self): 
     # use self.attribute for test 

的測試方法實例傳遞給setup_methodteardown_method,但可以如果您的設置/拆卸代碼不需要知道測試環境,則忽略它。更多的信息可以在here找到。

我還建議您熟悉py.test的fixtures,因爲它們是更強大的概念。

+0

爲什麼你寫'class Test'而不是'class Test(object)'? – 2017-09-26 14:14:48

+0

我主要在Python 3中顯示我的例子,這就是爲什麼我沒有打擾。 :) – 2017-09-27 15:43:13

+0

哦?我以爲應該寫'class Test(object)',但我不太確定我從哪裏讀取。 – 2017-09-27 15:50:31

10

正如@Bruno所建議的,使用pytest fixtures是另一種解決方案,既可以用於測試類,也可以用於簡單的測試函數。 Here's an example testing python2.7 functions

import pytest 

@pytest.fixture(scope='function') 
def some_resource(request): 
    stuff_i_setup = ["I setup"] 

    def some_teardown(): 
     stuff_i_setup[0] += " ... but now I'm torn down..." 
     print stuff_i_setup[0] 
    request.addfinalizer(some_teardown) 

    return stuff_i_setup[0] 

def test_1_that_needs_resource(some_resource): 
    print some_resource + "... and now I'm testing things..." 

因此,運行test_1...生產:

I setup... and now I'm testing things... 
I setup ... but now I'm torn down... 

注意stuff_i_setup在夾具中引用,使該對象成爲它與交互的測試setuptorn down。您可以想象這對於持久對象(如假設的數據庫或某個連接)很有用,必須在每次測試運行之前將其清除,以使它們保持隔離狀態。

23

根據Fixture finalization/executing teardown code使用addfinalizer是「歷史」。

作爲歷史記錄,另一種編寫拆解代碼的方法是通過接受請求對象到你的fixture函數中並調用它的請求。addfinalizer一次或多次:

的建立和拆除目前最好的做法是使用yield

import pytest 

@pytest.fixture() 
def resource(): 
    print("setup") 
    yield "resource" 
    print("teardown") 

class TestResource(object): 
    def test_that_depends_on_resource(self, resource): 
     print("testing {}".format(resource)) 

運行它導致

$ py.test --capture=no pytest_yield.py 
=== test session starts === 
platform darwin -- Python 2.7.10, pytest-3.0.2, py-1.4.31, pluggy-0.3.1 
collected 1 items 

pytest_yield.py setup 
testing resource 
.teardown 


=== 1 passed in 0.01 seconds === 
+0

因此,您將其複製到每個測試文件中,您將需要該資源? – 2017-11-11 02:00:43

+0

@AndyHayden根據你如何編寫你的燈具,你可以把它放到你需要它的每個測試文件中,或者你可以把它放在一個conftest.py文件中https://stackoverflow.com/questions/34466027/in-py- test-what-is-the-use-conftest-py-files – 2017-11-13 15:29:54

+0

然而,這不是一個類的設置,對吧?它會在類中的每個測試方法之前執行。 – malhar 2017-11-15 17:00:50

1

這可能有助於http://docs.pytest.org/en/latest/xunit_setup.html

在我的測試套件,我組我的測試案例爲類。爲了設置和拆卸,我需要該類中的所有測試用例,我使用setup_class(cls)teardown_class(cls)類方法。

而且對於建立和拆除我需要爲每個測試用例的,我用的是setup_method(method)teardown_method(methods)

例子:

lh = <got log handler from logger module> 

class TestClass: 
    @classmethod 
    def setup_class(cls): 
     lh.info("starting class: {} execution".format(cls.__name__)) 

    @classmethod 
    def teardown_class(cls): 
     lh.info("starting class: {} execution".format(cls.__name__)) 

    def setup_method(self, method): 
     lh.info("starting execution of tc: {}".format(method.__name__)) 

    def teardown_method(self, method): 
     lh.info("starting execution of tc: {}".format(method.__name__)) 

    def test_tc1(self): 
     <tc_content> 
     assert 

    def test_tc2(self): 
     <tc_content> 
     assert 

現在,當我跑我的測試中,當識別TestClass執行是它會記錄開始執行的時間,執行結束的時間和方法的詳細信息。

您可以在其他位置添加其他設置和拆卸步驟。

希望它有幫助!