Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

python - Why would a fixture reference not be able to access a table it created in a unit test?

So I attempted to to create a dynamodb table to run my tests with a pytest fixture like so:

@pytest.fixture
def db_resource():
    db_resource = boto3.resource('dynamodb')
    table = db_resource.create_table(
       ...

    return db_resource

Then I passed it into my test like so:

@mock_dynamodb2
def test_my_code(db_resource):
   other_func(db_resource)

This throws an error saying the table I just created does not exist (this is the first time I've tried doing it this way). However if I change it to a function that generates the table it works fine.

@mock_dynamodb2
def _create_table():
   db_resource = boto3.resource('dynamodb')
    table = db_resource.create_table(
       ...

   return db_resource

and call it in my unit test function...

@mock_dynamodb2
def test_my_code():
    db_resource = _create_table()
    other_func(db_resource)

This works fine. What am I missing?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...