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.

Unexpected behavior with @mock_s3 for a test suite

See original GitHub issue

Hey folks!

I was bitten by a weird behavior of moto today, I decided to take some time and build a test case for the behavior I’d expect:

import boto
import unittest
from moto import mock_s3


@mock_s3
class TestWithSetup(unittest.TestCase):
    def setUp(self):
        conn = boto.connect_s3()
        conn.create_bucket('mybucket')

    def test_should_find_bucket(self):
        conn = boto.connect_s3()
        self.assertIsNotNone(conn.get_bucket('mybucket'))

    def test_should_not_find_unknown_bucket(self):
        conn = boto.connect_s3()
        with self.assertRaises(Exception):
            conn.get_bucket('unknown_bucket')


@mock_s3
class TestWithPublicMethod(unittest.TestCase):
    def ensure_bucket_exists(self):
        conn = boto.connect_s3()
        conn.create_bucket('mybucket')

    def test_should_find_bucket(self):
        self.ensure_bucket_exists()

        conn = boto.connect_s3()
        self.assertIsNotNone(conn.get_bucket('mybucket'))

    def test_should_not_find_bucket(self):
        conn = boto.connect_s3()
        self.assertIsNone(conn.get_bucket('mybucket'))


@mock_s3
class TestWithPseudoPrivateMethod(unittest.TestCase):
    def _ensure_bucket_exists(self):
        conn = boto.connect_s3()
        conn.create_bucket('mybucket')

    def test_should_find_bucket(self):
        self._ensure_bucket_exists()
        conn = boto.connect_s3()
        self.assertIsNotNone(conn.get_bucket('mybucket'))

    def test_should_not_find_bucket(self):
        conn = boto.connect_s3()
        self.assertIsNone(conn.get_bucket('mybucket'))

This is related to commit https://github.com/spulec/moto/commit/3ed9428cb01cc0af157dfaf50fc4550e1a6d3ab5 for the issue https://github.com/spulec/moto/issues/363 – I think this fix is not the best way, because it seems to be making the contents of the bucket global to the test suite, while the expected IMO would be to make it local to each test method.

Before that change, it seems that the contents of the bucket was indeed local to every method, but the result was unexpected because it was isolating also setUp and other non-test methods.

I’m not sure how to fix it, I hope the test case helps at least to decide which is the expected behavior.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
suederadecommented, Apr 13, 2018

Has this been changed?

1reaction
eliasdornelescommented, Apr 6, 2022

Hello! 👋 I’ve came back just to say thanks! Have a good day! 😀

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to mock S3 services in Python tests | by Lorenzo Peppoloni
In our case, we are going to setup a mock S3 environment in the setUp ... the world in an unexpected state, due...
Read more >
Mimicking Production Behavior with Generated Mocks - arXiv
Abstract—Mocking in the context of automated software tests allows testing program units in isolation. Designing realistic interactions be-.
Read more >
Testing behavior of a mocked S3 bucket using s3fs (by dask ...
Likely what is going on, is that the s3 object in the mock_s3 function is caching the directory listing for performance reasons.
Read more >
com.amazonaws.services.s3.model.AccessControlList.<init ...
@Test public void testSetACL() { AccessControlList acl = new AccessControlList(); EasyMock.reset(mockS3); EasyMock.expect(mockS3.getRegion()).
Read more >
Mock S3 for integration test - DEV Community ‍ ‍
Although we can use S3 mock library (e.g. adobe/S3Mock), sometimes we just want to make thing... Tagged with java, aws, s3, testing.
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