Home > Software engineering >  Unit test failure due to second positional argument. Simple math result comparison?
Unit test failure due to second positional argument. Simple math result comparison?

Time:08-30

I am creating unit tests by using the unittest module, and simply asserting Equal a result that is correct when calculating the Fibonacci sequence. Below is the working code- the 5th number is 2 3 = 5.

    num1 = 0
    num2 = 1
    find = 2
    fib_num = 0

    while find <= N:
        fib_num = num1   num2
        num1 = num2
        num2 = fib_num
        find = find   1
    return(fib_num)


print(fibFind(5))

This is mu Unittest


from fibfind import fibFind


class TestFibonnaciResult(unittest.TestCase):
    def test_number(self):
        self.assertEqual(fibFind(5)== 5),
        self.assertEqual(fibFind(6)==8),
        self.assertEqual(fibFind(7)== 13)
        5, 8, 13

    def test_input_type(self):
        self.assertTrue(fibFind("some-string") == error)


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

In the terminal however, I get this?

$ python -m unittest test_fibFind.py
EE
======================================================================
ERROR: test_input_type (test_fibFind.TestFibonnaciResult)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\XYZ\dev\repos\testing_python\test_fibFind.py", line 14, in test_input_type
    self.assertTrue(fibFind("some-string") == error)
  File "C:\Users\XYZ\dev\repos\testing_python\fibfind.py", line 7, in fibFind
    while find <= N:
TypeError: '<=' not supported between instances of 'int' and 'str'

======================================================================
ERROR: test_number (test_fibFind.TestFibonnaciResult)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\XYZ\dev\repos\testing_python\test_fibFind.py", line 8, in test_number
    self.assertEqual(fibFind(5)== 5),
TypeError: assertEqual() missing 1 required positional argument: 'second'

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (errors=2)
5

The second test is wanting to make sure only an integer is parsed through but clearly is having issues with the comparator <=

Any help here before I move onto pytest would be great.

CodePudding user response:

Other answers are mostly correct, but none touches on the other test that expects an exception to be raised. unittest assert... methods have very specific signatures, which you should check before using. In the first test, you basicly did the following:

self.assertTrue(True)

Doesn't make much sense, right? Instead, you should pass 2 objects as a first and second arguments, and let unittest handle their comparison.

self.assertEqual(fibFind(5), 5)

Similiarly, with a bad input test you can't simply call a function with bad arguments and compare to undefined error. There exists assertRaises method exactly for that purpose. It accepts an exception type, callable and arguments that will be passed to the callable. Which means you should call it like that:

self.assertRaises(TypeError, fibFind, "some-string")

All in all, your test class should look like that:

class TestFibonnaciResult(unittest.TestCase):
    def test_number(self):
        self.assertEqual(fibFind(5), 5)
        self.assertEqual(fibFind(6), 8)
        self.assertEqual(fibFind(7), 13)

    def test_input_type(self):
        self.assertRaises(TypeError, fibFind, "some-string")

Output:

5
..
----------------------------------------------------------------------
Ran 2 tests in 0.024s

OK

CodePudding user response:

Python-unittest offers you two options to do what you're trying to achieve.

If you want to write a boolean expression to be compared, you can use the methods assertTrue or assertFalse (documentation).

class TestFibonnaciResult(unittest.TestCase):
    def test_number(self):
        self.assertTrue(fibFind(5)== 5),
        self.assertTrue(fibFind(6)==8),
        self.assertTrue(fibFind(7)== 13)

You can also use assertEqual, which takes 2 parameters, first and second, and compares them (documentation).

class TestFibonnaciResult(unittest.TestCase):
    def test_number(self):
        self.assertEqual(fibFind(5),5),
        self.assertEqual(fibFind(6),8),
        self.assertEqual(fibFind(7),13)

CodePudding user response:

assertEqual requires 2 arguments, You're only giving it one argument, because your expression evalutes to a True or False value. Try this instead:

def test_number(self):
    self.assertEqual(fibFind(5), 5),
    self.assertEqual(fibFind(6), 8),
    self.assertEqual(fibFind(7), 13)

CodePudding user response:

In mu Unittest line 8, 9, 10 assertEqual takes two arguments not one You also cannot compare an int and a string

  • Related