【发布时间】:2015-11-05 15:42:26
【问题描述】:
我有一个需要来自测试函数的变量的夹具。如果函数级别的自省有效,则使用自省并在函数命名空间/上下文中声明变量应该可以工作,就像它在模块级别一样,但是每次我运行代码时,我都会以 None 而不是字符串“Fancy Table”结束。
在夹具中,我将范围设置为“函数”,然后通过 getattr 和 request.function 进行内省:
#conftest.py
@pytest.fixture(scope='function')
def table(request):
from data_setup import create_table
table_name = getattr(request.function, "table_name", None)
create_table(request, table_name)
我在测试函数中声明了变量table_name:
#test_file.py
class TestTable():
@pytest.mark.tags("table")
def test_create_table(self, test_db):
table_name = "Fancy Table"
current_page = TablePage(self.test_driver, test_db)
current_page.go_to_kitchen("Eva", "Evas Kitchen")
current_page.create_first_table(expected_table_name)
Validation.assert_equal(expected_table_name, current_page.get_name(), "Table had the wrong name!")
在模块级别上这样做是有效的,就像类一样,但是一旦我尝试在功能级别上这样做,夹具就会再次吐出 None 。我在功能级别上使用夹具自省错误吗?不这样怎么用?
【问题讨论】:
标签: python pytest fixtures introspection