Amazon web services Moto不适用于python单元测试设置()调用
我正在使用moto和Python3.7模拟一些S3交互。如果测试方法中包含所有模拟代码,那么moto似乎工作正常。当我将一些预备代码移动到Amazon web services Moto不适用于python单元测试设置()调用,amazon-web-services,boto3,moto,Amazon Web Services,Boto3,Moto,我正在使用moto和Python3.7模拟一些S3交互。如果测试方法中包含所有模拟代码,那么moto似乎工作正常。当我将一些预备代码移动到setUp()时,测试失败,就好像setUp()从未运行过一样 import unittest import boto3 from moto import mock_s3 class BucketFacadeTests(unittest.TestCase): @mock_s3 def setUp(self): print(
setUp()
时,测试失败,就好像setUp()
从未运行过一样
import unittest
import boto3
from moto import mock_s3
class BucketFacadeTests(unittest.TestCase):
@mock_s3
def setUp(self):
print('setUp called')
s3 = boto3.resource('s3', region_name='us-east-1')
s3.create_bucket(Bucket='bucket')
key = 'a/b/c/d.txt'
object = s3.Object('bucket', key)
object.put(Body='my dog has fleas')
def do_test(self):
s3 = boto3.resource('s3', region_name='us-east-1')
the_object = s3.Object('bucket', 'a/b/c/d.txt')
string_data = the_object.get()['Body'].read().decode('utf-8')
self.assertEqual('my dog has fleas', string_data)
@mock_s3
def test_bucket_can_be_accessed_with_setup(self):
self.do_test()
@mock_s3
def test_bucket_can_be_accessed_without_setup(self):
# This does what setUp() should
s3 = boto3.resource('s3', region_name='us-east-1')
s3.create_bucket(Bucket='bucket')
key = 'a/b/c/d.txt'
object = s3.Object('bucket', key)
object.put(Body='my dog has fleas')
self.do_test()
当我不依赖setUp()
时,一切都按预期运行
Testing started at 07:49 ...
/Users/paul/.virtualenvs/nui-converter/bin/python "/Applications/PyCharm CE.app/Contents/helpers/pycharm/_jb_unittest_runner.py" --target BucketFacade2Tests.BucketFacadeTests.test_bucket_can_be_accessed_without_setup
Launching unittests with arguments python -m unittest BucketFacade2Tests.BucketFacadeTests.test_bucket_can_be_accessed_without_setup in /Users/Paul/as/nui-converter/tests/InventoryLoader
setUp called
Ran 1 test in 0.103s
OK
Process finished with exit code 0
然而,当我依赖setUp()
我是做错了什么还是把moto推到了极限之外了?问题是您正在将模拟s3装饰器直接应用到
setUp()
方法和测试方法中。这会导致单独的模拟s3环境,因此不会共享在setUp()
方法中完成的任何操作
解决方案是将@mock_s3
装饰器应用于整个BucketFacadeTests
类
下面的代码应按预期工作
import unittest
import boto3
from moto import mock_s3
@mock_s3
class BucketFacadeTests(unittest.TestCase):
def setUp(self):
print('setUp called')
s3 = boto3.resource('s3', region_name='us-east-1')
s3.create_bucket(Bucket='bucket')
key = 'a/b/c/d.txt'
object = s3.Object('bucket', key)
object.put(Body='my dog has fleas')
def do_test(self):
s3 = boto3.resource('s3', region_name='us-east-1')
the_object = s3.Object('bucket', 'a/b/c/d.txt')
string_data = the_object.get()['Body'].read().decode('utf-8')
self.assertEqual('my dog has fleas', string_data)
def test_bucket_can_be_accessed_with_setup(self):
self.do_test()
def test_bucket_can_be_accessed_without_setup(self):
# This does what setUp() should
s3 = boto3.resource('s3', region_name='us-east-1')
s3.create_bucket(Bucket='bucket')
key = 'a/b/c/d.txt'
object = s3.Object('bucket', key)
object.put(Body='my dog has fleas')
self.do_test()
import unittest
import boto3
from moto import mock_s3
@mock_s3
class BucketFacadeTests(unittest.TestCase):
def setUp(self):
print('setUp called')
s3 = boto3.resource('s3', region_name='us-east-1')
s3.create_bucket(Bucket='bucket')
key = 'a/b/c/d.txt'
object = s3.Object('bucket', key)
object.put(Body='my dog has fleas')
def do_test(self):
s3 = boto3.resource('s3', region_name='us-east-1')
the_object = s3.Object('bucket', 'a/b/c/d.txt')
string_data = the_object.get()['Body'].read().decode('utf-8')
self.assertEqual('my dog has fleas', string_data)
def test_bucket_can_be_accessed_with_setup(self):
self.do_test()
def test_bucket_can_be_accessed_without_setup(self):
# This does what setUp() should
s3 = boto3.resource('s3', region_name='us-east-1')
s3.create_bucket(Bucket='bucket')
key = 'a/b/c/d.txt'
object = s3.Object('bucket', key)
object.put(Body='my dog has fleas')
self.do_test()