2016-05-08 14 views
0

新しいpytest(およびPython)に追加されました。すべての私のテストの前に一度だけ実行されるもののセットを持っている(例えば、 - アンドロイドエミュレータの起動、appiumドライバの作成、すべてのページクラスをインスタンス化してテストで使用できるようにする)。 Btw、私は複数のクラスで自分のテストをしています。読書のビットの後、私はpy.testメソッドは1回の実行につき1回だけ実行されます

import pytest 

class TestBase(): 
    @pytest.yield_fixture(scope="session", autouse=True) 
    def fixture_session(self): 
     # start emulator, create driver and instantiate all page 
     # classes with driver create above 
     print "\n in fixture_session! @session " 
     yield 
     # tear down 
     print "in fixture_session after yield @session" 
    @pytest.yield_fixture(scope="module", autouse=True) 
    def fixture_module(request): 
     print 'in fixture_module @module' 
     # tear down 
     yield 
     print "in fixture_module after yield @module" 

class TestOne(TestBase): 
    def test_a(self): 
     # write test with page objects created in TestBase 
     print "in test_a of TestOne" 
    def test_b(self): 
     print "in test_b of TestOne" 

class TestTwo(TestBase): 
    def test_a(self): 
     print "in test_a of TestTwo" 

    def test_b(self): 
     print "in test_b of TestTwo" 

は、私が何をしないのです、これは

test_base.py 
in fixture_session! @session 
in fixture_module @module 
in test_a of TestOne 
.in test_b of TestOne 
. 
in fixture_session! @session 
in fixture_module @module 
in test_a of TestTwo 
.in test_b of TestTwo 
.in fixture_module after yield @module 
in fixture_module after yield @module 
in fixture_session after yield @session 
in fixture_session after yield @session 

を与えて実行する。..例は、以下を参照してください.. @pytest.yield_fixture(scope="session", autouse=True)は、トリックを行うだろうと思ったが、私が見るものではないthatsの?なぜ、@pytest.yield_fixture(scope="session", autouse=True)がテストクラスごとに実行されているのですか?そして、完全なテスト実行の後に引き裂かれるのはなぜですか?まったく、Pytestでテストフレームワークを設定するこの正しい方法はありますか?

答えて

0

これは、クラス内にフィクスチャを定義してサブクラス化し、フィクスチャを2回定義するためです。

代わりに、フィクスチャを通常の関数として定義し、テストを関数に変更するか、テストをグループ化するためにクラスを使用する場合はベースクラスから継承しないでください。

関連する問題