Issue with Moto and creating pytest fixture
See original GitHub issueHello
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:
- Created 7 years ago
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For pytest, I’ve had success making a fixture like this:
And then just using the fixture in my tests:
You don’t want to
mock_s3()
again before your test.Using a context manager: