Pytest - Fixture introspect on function level

浪子不回头ぞ 提交于 2019-12-06 03:22:01

Function variables are local and are destroyed after the function returns, they are not bound the function object in any way... this is how Python works and not related to py.test.

Your example will work if you bind the table_name local variable explicitly to the test function, effectively letting it outlive its usual life-time:

@pytest.mark.tags("table")
def test_create_table(self, test_db):
    test_create_table.table_name = "Fancy Table"

On the other hand, wouldn't be simpler to just pass the table_name to TablePage explicitly? It would be simpler, straightforward and, well, explicit. :)

Explicitly binding the local variable to the test function resulted in my IDE complaining about an Unresolved reference. What else do we need to know to make this work?

In the example given, when I write test_create_table.table_name = "Fancy Table", the test_create_table part is the part my IDE complains has an Unresolved reference. So given I am receiving the Unresolved reference error, how can I successfully explicitly bind the local variable?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!