question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with Moto and creating pytest fixture

See original GitHub issue

Hello

I have been trying to create a pytest fixture for s3 that contains a bucket that is created in the fixture.

@pytest.fixture
@mock_s3
def s3_hook():

    result = sut.S3Hook()
    result.location = 'test_location'

    result.connection = boto3.client('s3')
    result.connection.create_bucket(Bucket=result.location)
    return result

My test looks like this:

@mock_s3
def test_check_existing_file(s3_hook):

    saved_string = "Just writing some tests."

    s3_hook.connection.put_object(Bucket=s3_hook.location,
                                  Key='test_file',
                                  Body=saved_string)

    assert s3_hook.check_for_file('test_file') 

This fails with the message

<Message>The specified bucket does not exist</Message>

If I update the test to create the bucket at the start of the test the test starts passing.

s3_hook.connection.create_bucket(Bucket=s3_hook.location)

Any thoughts on this?

I am using: pytest==2.8.0 moto==0.4.24

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

37reactions
mikegrimacommented, Jun 7, 2016

For pytest, I’ve had success making a fixture like this:

@pytest.yield_fixture(scope="function")
def s3_fixture():
    mock_s3().start()

    client = boto3.client("s3")
    resource = boto3.resource("s3")

    yield client, resource

    mock_s3().stop()

And then just using the fixture in my tests:

def test_s3_thing(s3_fixture):
    client = s3_fixture[0]
    client.create_bucket(Bucket="some-test-bucket")

You don’t want to mock_s3() again before your test.

2reactions
Peradoscommented, Apr 14, 2020

Using a context manager:

@pytest.yield_fixture(scope="function")
def s3_fixture():
    with mock_s3():
        client = boto3.client("s3")
        resource = boto3.resource("s3")

        yield client, resource
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Testing with Pytest and Moto | by TensorIoT Editor - Medium
It is perfectly fine to create, interact, assert, with a test instance. Combine Pytest Fixtures with Moto. Now combine everything we've covered ...
Read more >
How to test your AWS code using Moto and Pytest - Learn AWS
Moto works well with Pytest fixtures and makes testing AWS functionality pretty straightforward.
Read more >
mock_s3 decorating pytest fixture - python - Stack Overflow
The problem is that, client.list_objects is not mocked in the test which uses fixture. pytest - 3.1.2 moto - 1.0.1 boto3 - 1.0.4....
Read more >
Mocking AWS in Python Using moto and pytest Monkeypatching
The create_test_secret pytest fixture takes the above mocked AWS SM instance and uses it to create a fake secret ( mock_secret ) that...
Read more >
pytest fixtures: explicit, modular, scalable
We have to make the test “request” the fixtures it depends on, ... If an earlier fixture has a problem, though, and raises...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found