简体   繁体   中英

Factorial Unit Test function returning wrong test runs

I am trying to create a unittest using the input value. I know that math.factorial function will give the correct number so I would like to pass the same value to my factorial function that I created and the built in math.factorial function to compare the results and see if my function is working fine.

What I don't understand is that somehow the code below says RAN OK is 0.000s even when I enter a negative value. I believe in that case it should not work.[funny thing some times it say ran ok with negative and sometimes not]

Additionally, If I use the second code below it still says pass on a test when I enter a negative number. Even though I have removed the first IF statement

and the third thing that is not clear to me is about how does the class TestCalc accepts the input variable ie "n". I thought that I would need to create another parameter like (self, number). Somehow it is accepting n in the def_test_factorial(self)

I am new to this unit testing and classes and objects. I was hoping if someone can clear these doubts.

from functools import reduce
import doctest
import time
import math
import numpy as np
import unittest

def calculate_factorial(number):
    if number < 0:
        raise ValueError('number should be greater than 0')
    elif type(number) != int:
        raise  TypeError('number should be an integer type')
    else:
        data = []
        for i in range(number):
            data.append(number - i)
            # print(data)
        results = reduce((lambda x, y: x * y), data, 1)
        return results




run = True
while run:
    n = int(input('Enter an integer value: '))
    if n != -9999:
        unittest.main(argv=[''], verbosity=2, exit=False)  
    else:
        run = False


class TestCalc(unittest.TestCase):
    def test_factorial(self):
        result = calculate_factorial(n)
        self.assertEqual(result, math.factorial(n))

    def test_n(self):
        print(n)

second code:

def calculate_factorial_(number):
    if type(number) != int:
        raise  TypeError('number should be an integer type')
    else:
        data = []
        for i in range(number):
            data.append(number - i)
            # print(data)
        results = reduce((lambda x, y: x * y), data, 1)
        return results

I have removed the negative number condition and it still shows the results

run = True
while run:
    n = int(input('Enter an integer value: '))
    if n != -9999:
        unittest.main(argv=[''], verbosity=2, exit=False)
    else:
        run = False


class TestCalc(unittest.TestCase):
    def test_factorial(self):
        result = calculate_factorial_(n)
        self.assertEqual(result,math.factorial(n))
    
    def test_n(self):
        print(n)

I solved the above by moving the class function before the while loop.

from functools import reduce
import doctest
import time
import math
import numpy as np
import unittest

def calculate_factorial(number):
    if number < 0:
        raise ValueError('number should be greater than 0')
    elif type(number) != int:
        raise  TypeError('number should be an integer type')
    else:
        data = []
        for i in range(number):
            data.append(number - i)
            # print(data)
        results = reduce((lambda x, y: x * y), data, 1)
        return results


class TestCalc(unittest.TestCase):
    def test_factorial(self):
        result = calculate_factorial_(n)
        self.assertEqual(result,math.factorial(n))
    
    def test_n(self):
        print(n)

run = True
while run:
    n = int(input('Enter an integer value: '))
    if n != -9999:
        unittest.main(argv=[''], verbosity=2, exit=False)
    else:
        run = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM