2
我有以下結構:爲什麼不能測試函數看我的py.test夾具?
demo/
conftest.py
test_1.py
test_2.py
conftest.py
與含量爲:
import pytest
@pytest.fixture()
def my_fixture():
print "testing"*
和test_1.py
含量爲:
def test_my_fixture(my_fixture):
my_fixture
和test_2.py
含量爲:
import pytest
@pytest.mark.usefixtures('my_fixture')
def test_my_fixture():
my_fixture
當執行測試py.test -s
時,我得到NameError
爲夾具在test_2.py
但不在test_1.py
;爲什麼?
這裏是輸出:
================ test session starts ================
platform linux2 -- Python 2.7.3 -- py-1.4.26 -- pytest-2.6.4
plugins: timeout, random
collected 2 items
test_1.py testing
.
test_2.py testing
F
===================== FAILURES ======================
__________________ test_my_fixture __________________
def test_my_fixture():
> my_fixture
E NameError: global name 'my_fixture' is not defined
test_2.py:7: NameError
在這裏,我使用my_fixture作爲一個通用的設置函數,以便在需要時調用我所有的測試。我不需要直接訪問燈具對象。就像http://pytest.org/latest/fixture.html#using-fixtures-from-classes-modules-or-projects – 2015-02-09 14:18:52
@reddymr中的cleandir夾具一樣,如果你用'pytest.mark.usefixtures(' my_fixture')','my_fixture' fixture函數被調用,你可以在輸出中看到。 – falsetru 2015-02-09 14:26:58
當我在test_2.py中使用@ pytest.mark.usefixtures('my_fixture')時,它不會被調用,但在用作測試函數test_my_fixture(my_fixture)的參數時會調用test_.py – 2015-02-09 16:11:07