[英]How should unit testing work with external resources? What is proper approach?
I have recently learned about unit testing and know that you shouldn't be unit testing external resources. 我最近了解了单元测试,并且知道您不应该对外部资源进行单元测试。 So, that leads me to a problem for testing a simple function that I want to rewrite to the standards of unit testing.
因此,这给我带来了一个测试简单功能的问题,我想将其重写为单元测试的标准。
An example of the function is below for simplicity sake: 为了简单起见,下面是该函数的一个示例:
def longTask(somearg):
# Check somearg
# Begin infinite loop
# Check remote resource loop based on somearg
# Write results to a database or file
# Check to break loop
# Cleanup upon end
I want to make sure the above code has been unit tested (now that I know of unit testing). 我想确保上面的代码已经过单元测试(现在我知道单元测试了)。
My main confusion comes from the fact of how can I unit test simple functions that are making external resource calls when you aren't supposed to unit test external resources? 我的主要困惑来自以下事实:当您不应该对外部资源进行单元测试时,如何对进行外部资源调用的简单函数进行单元测试?
Note: I have read the other posts about this on SO but those do not answer my question. 注意:我已经阅读了有关SO的其他文章,但这些文章没有回答我的问题。
My main confusion comes from the fact of how can I unit test simple functions that are making external resource calls when you aren't supposed to unit test external resources?
What I usually do in cases such as this is use a mock of some sort. 在这种情况下,我通常要做的是使用某种模拟。 There are some excellent mock packages for python, for instance http://www.voidspace.org.uk/python/mock that make these kind of substitutions of a test object for a real object much easier
有一些出色的python模拟包,例如http://www.voidspace.org.uk/python/mock ,它们使测试对象对真实对象的这种替换变得更加容易
For example 例如
def test_au(self):
user_id=124
def apple(req):
return 104
with patch('pyramid.security.authenticated_userid', return_value=user_id
):
authenticated_userid = apple
self.assertEqual("104", authenticated_userid(self.request).__str__()
)
patch is a method imported from mock. 补丁是从模拟导入的方法。 It alters the behaviour of other packages within the scope it is given.
它会在给定范围内更改其他软件包的行为。
In this example, the predefined library method for authenticated_userid is imported with from pyramid.security import authenticated_userid
and works within the pyramid framework. 在此示例中,authenticated_userid的预定义库方法是
from pyramid.security import authenticated_userid
并在金字塔框架内工作。 To test that it is returning the correct value after my setup function has run I can "override" the method during the test 为了在我的设置函数运行后测试它是否返回正确的值,我可以在测试过程中“覆盖”该方法
Have you considered using something like a Fake object [1] to test your code. 您是否考虑过使用诸如Fake对象[1]之类的东西来测试您的代码。 You could provide a wrapper/interface for the external resource and for your tests, use a version of that wrapper/interface that offers behavior to facilitate testing.
您可以为外部资源和测试提供包装器/接口,使用该包装器/接口的版本提供行为以方便测试。
[1] https://en.wikipedia.org/wiki/Mock_object#Mocks.2C_fakes_and_stubs [1] https://zh.wikipedia.org/wiki/Mock_object#Mocks.2C_fakes_and_stubs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.