Unexpected behavior with @mock_s3 for a test suite
See original GitHub issueHey 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:
- Created 8 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Has this been changed?
Hello! 👋 I’ve came back just to say thanks! Have a good day! 😀