使用unittest模块进行Python单元测试

发布于 2025-01-20 05:49:16 字数 855 浏览 2 评论 0原文

这就是我编写代码以使用 Unitsest 模块开始测试的方式,但它作为0个测试返回。回报有任何问题吗? (我可以共享完整的代码,但很长)。发布代码和下面的脚本:

  1. 脚本:

      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) 
     
  2. 显示错误的0个测试:

     在0.000中进行0测试
    
    好的
     
  3. 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:

  1. 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) 
    
  2. Showing the error with 0 tests:

    Ran 0 tests in 0.000s
    
    OK
    
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2025-01-27 05:49:17

测试函数应以 test 开头:

class Test1(unittest.TestCase):
 ...
 def test_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) 
 ...
if __name__ == '__main__':
     unittest.main()

随意检查 有关 unittest 的文档

The test function should start with test:

class Test1(unittest.TestCase):
 ...
 def test_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) 
 ...
if __name__ == '__main__':
     unittest.main()

Feel free to check the documentation regarding unittest

裂开嘴轻声笑有多痛 2025-01-27 05:49:17

看来您的功能可以执行某些操作,并且想测试它。在这种情况下,您需要将此功能从课堂上删除,在课堂上,测试本身会写下测试,而不是您正在测试的功能。测试方法不能接受诸如temps和predition_month之类的参数。
您的功能返回某些内容,在测试中,我们可以将某些值传递到其中,并检查其是否返回了预期的结果:

import unittest


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) 

class Test1(unittest.TestCase):
    
    def test_get_avg(self):
        # your test code
        self.assertEqual(get_avg(temps test value, predict_month test value), expected_result)

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:

import unittest


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) 

class Test1(unittest.TestCase):
    
    def test_get_avg(self):
        # your test code
        self.assertEqual(get_avg(temps test value, predict_month test value), expected_result)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文