简体   繁体   English

因子单元测试 function 返回错误的测试运行

[英]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.我知道 math.factorial function 会给出正确的数字,所以我想将相同的值传递给我创建的阶乘 function 和内置的 math.factorial function 来比较结果,看看我的 function 是否工作正常。

What I don't understand is that somehow the code below says RAN OK is 0.000s even when I enter a negative value.我不明白的是,即使我输入负值,下面的代码也会以某种方式说 RAN OK 是 0.000s。 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即使我删除了第一个 IF 语句

and the third thing that is not clear to me is about how does the class TestCalc accepts the input variable ie "n".我不清楚的第三件事是 class TestCalc 如何接受输入变量,即“n”。 I thought that I would need to create another parameter like (self, number).我认为我需要创建另一个参数,如 (self, number)。 Somehow it is accepting n in the def_test_factorial(self)它以某种方式在 def_test_factorial(self) 中接受 n

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.我通过在 while 循环之前移动 class function 解决了上述问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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