使用unittest模块进行Python单元测试
这就是我编写代码以使用 Unitsest 模块开始测试的方式,但它作为0个测试返回。回报有任何问题吗? (我可以共享完整的代码,但很长)。发布代码和下面的脚本:
脚本:
class test1(unittest.testcase): def get_avg(temps,preditive_month): #print(“ temp:==>>>“,temps”,precenta _ month:=>>>>>“,precenta temp_arr = [] idx_num = month_dict [prectiv_month] temp_arr.append(float(temps [idx_num]))) 对于我的范围(0、5、1): idx_num += 1 idx_num = idx_num%12 temp_arr.append(float(temps [idx_num]))) 经过 #返回np.average(temp_arr,axis = 0)
显示错误的0个测试:
在0.000中进行0测试 好的
i最终运行了主 Unitsest 以下内容:
如果__name__ =='__ -main __': UNITEST.MAIN()
。
This is how I wrote the code to start a test with the unittest module but it is returning as 0 tests. Is the return making any problems? (I am able to share the complete code but it is long). Posting the code and the script below:
Script:
class Test1(unittest.TestCase): def get_avg(temps, predict_month): #print("Temp:==>>>",temps,"predict_month:=>>>>",predict_month) temp_arr = [] idx_num = month_dict[predict_month] temp_arr.append(float(temps[idx_num])) for i in range (0, 5, 1): idx_num += 1 idx_num = idx_num % 12 temp_arr.append(float(temps[idx_num])) pass # return np.average(temp_arr, axis=0)
Showing the error with 0 tests:
Ran 0 tests in 0.000s OK
I ran the main unittest at the end with this:
if __name__ == '__main__': unittest.main()
I want to know about my faults and loopholes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试函数应以
test
开头:随意检查 有关
unittest
的文档The test function should start with
test
:Feel free to check the documentation regarding
unittest
看来您的功能可以执行某些操作,并且想测试它。在这种情况下,您需要将此功能从课堂上删除,在课堂上,测试本身会写下测试,而不是您正在测试的功能。测试方法不能接受诸如temps和predition_month之类的参数。
您的功能返回某些内容,在测试中,我们可以将某些值传递到其中,并检查其是否返回了预期的结果:
It looks like you have a function that does something and you want to test it. In this case, you need to take this function out of the class, and in the class with tests write the tests themselves, and not the function that you are testing. Test method cannot accept arguments like temps and predict_month.
Your function returns something, and in the test we can pass certain values into it and check that it returned the expected result: