pytest parameterized method setup

萝らか妹 提交于 2019-12-13 07:19:17

问题


I have a parameterized pytest test method, test_1. Before all the parameterized cases are run for this test method, I'd like to call another method, tmp_db_uri, which creates a temporary database and yields the uri for the database. I only want to call that generator once, so that I can use the same temporary database for all the test cases. I thought that if I called it from a fixture (db_uri), that would do the trick, since I thought that fixtures are created once per test, but it seems that the fixture is getting called for each case in this test, and a new temporary database is being created each time.

What is the correct way to do this? Is there a way to run a setup for this method before all the cases are run, to use just one tmp_db_uri? I don't want the temporary database hanging around for the entire test module - just for the duration of this one test (cleanup is handled by a context manager on tmp_db_uri).

I currently have something that looks similar to this:

@pytest.fixture
def db_uri(tmp_db_uri):
    return tmp_db_uri

@pytest.mark.parameterize(("item1","item2"), ((1, "a"), (2, "b")))
def test_1(item1, item2, db_uri):
    print("do something")

回答1:


You can create a module level fixture ,so that it's created only once for the entire test module or you can create a global variable and return the db if it is already created or create otherwise.

@pytest.fixture(scope="module")
def db_uri(tmp_db_uri):
    return tmp_db_uri

or

TMP_DB = None
@pytest.fixture
def db_uri(tmp_db_uri):
    global TMP_DB
    if not TMP_DB:
        # do your stuff to create tmp_db 
        TMP_DB = tmp_db_uri
    return TMP_DB


来源:https://stackoverflow.com/questions/35211606/pytest-parameterized-method-setup

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