【问题标题】:Python unit testing with unittest module使用 unittest 模块进行 Python 单元测试
【发布时间】:2022-10-18 18:15:32
【问题描述】:

这就是我编写代码以开始测试的方式单元测试模块,但它返回为 0 个测试。退货有问题吗? (我可以分享完整的代码,但它很长)。在下面发布代码和脚本:

  1. 脚本:

    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. 使用 0 个测试显示错误:

    Ran 0 tests in 0.000s
    
    OK
    
  3. 我跑了主要单元测试最后是:

    if __name__ == '__main__':
         unittest.main()
    

    我想知道我的缺点和漏洞。

【问题讨论】:

    标签: python unit-testing testing automated-tests python-unittest


    【解决方案1】:

    测试函数应该以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()
    

    随时查看documentation regarding unittest

    【讨论】:

    • 谢谢你。实施它。同样的错误即将到来。
    • 嗯,我刚刚意识到您正在将一些参数传递给test_get_avg 函数。您应该创建一个不同的函数来将值分配为类属性,并且只传递“self”。检查this answer。所以把“test_”放在函数名的开头+传递正确的参数,它应该可以工作
    • 放一些随机值,检查更新的代码,让我知道它是否有帮助
    【解决方案2】:

    看起来你有一个做某事的函数,你想测试它。在这种情况下,您需要将此函数从类中取出,并在带有测试的类中编写测试本身,而不是您正在测试的函数。测试方法不能接受像 temps 和 predict_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)
    

    【讨论】:

      【解决方案3】:
       for(int i = 1 ; i < q.size() ; i++) {
              q.offer(q.poll());
      

      【讨论】:

      • 欢迎来到堆栈溢出!请阅读How to Answeredit 你的答案,以解释为什么这段代码实际上可以解决手头的问题。永远记住,您不仅在解决问题,而且还在教育 OP 和这篇文章的任何未来读者。
      猜你喜欢
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多