简体   繁体   English

为循环中的函数编写测试用例的最佳方法是什么

[英]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.

相关问题 在 Python 中对这个 function 进行单元测试的最佳方法是什么? - What's is the best way to unit test this function in Python? 无法修补/模拟从我正在编写单元测试用例的实例函数之一调用的实例函数 - Not able to patch/mock instance functions which called from one of instance function on which I am writing unit test case 在这种情况下,提取文本的最佳方法是什么? - What's the best way to extract the text in this case? 在抽象类的情况下,实例化的最佳方法是什么? - What is the best way for instantiation in the case of abstract classes? 在这种情况下,绕过“ MemoryError”的最佳方法是什么? - What could be the best way to bypass `MemoryError` in this case? 使用for和while循环编写素数函数的最Python方式是哪一种? - Which is the most pythonic way for writing a prime number function using for and while loop? 在python opencv中使用for循环的最佳方法是什么,将其输出(图像)保存在多个变量中? - What is the best way to use for loop in python opencv which saves its outputs(images) in multiple variables? 从Python测试用例中更改“设置”值的最佳方法是什么? - Best way to change the value of “settings” from within a Python test case? 从外部停止循环的最佳方法是什么? - What is the best way to stop a loop from outside it? 在python中循环该程序的最佳方法是什么? - What's the best way to loop this program in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM