0

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.

pppery
  • 3,731
  • 22
  • 33
  • 46
user18174484
  • 79
  • 1
  • 9

2 Answers2

0

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

Latra
  • 492
  • 3
  • 14
  • Thank you. Implemented it. The same error is coming. – user18174484 Apr 10 '22 at 13:08
  • Ey, I just realized that you are passing some parameters to `test_get_avg` function. You should create a different function to assign the values as class atribute, and pass only "self". Check [this answer](https://stackoverflow.com/questions/11380413/python-unittest-passing-arguments). So putting "test_" at the begining of the function name + passing the right parameters, it should work – Latra Apr 26 '22 at 13:52
  • puting some random values, check the updated code and let me know if it helped – Latra Apr 26 '22 at 14:01
0

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)
anfisa9la
  • 81
  • 4