[英]What is the best way of writing test case for function which is in loop
I want to write a testcase but that function is in loop我想写一个测试用例,但该函数在循环中
def myfunction():
for file in files:
myfun(file, Temp=True)
One important thing to understand here is that, you dont need to test the loop.这里要了解的一件重要事情是,您不需要测试循环。 What you need to do is test the function itself.
您需要做的是测试函数本身。
Thus write a test that tests myfun
.因此编写一个测试
myfun
测试。 Later on you can add more tests depending on the type of files that myfun
is expected to handle.稍后您可以根据
myfun
预期处理的文件类型添加更多测试。
If what you wanna test is really myfunction
, the simplest way is to replace the hardcoded call to myfun
by a callback, ie如果你想测试的真的是
myfunction
,最简单的方法是用回调替换对myfun
的硬编码调用,即
def myfunction(callback=myfun):
files = whatever()
for f in files:
callback(f, temp=True)
Then you can pass anything you want as callback for your tests.然后你可以传递任何你想要的作为你测试的回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.