这是我创建的函数:
def hab(h, a, b= None):
if b != None:
result = ("{} , {} , {}".format(h, b, a))
else:
result = ("{} , {}".format(h, a))
return result
我正在尝试为我的函数编写一个单元测试,单元测试应该在提供两个或三个参数时断言函数的正确性。
这是我的框架:
class hab_Test_Class(unittest.TestCase):
def test_pass2(self):
def test_pass3(self):
# i'll use code below to run the unit test
t = hab_Test_Class()
suite = unittest.TestLoader().loadTestsFromModule(t)
unittest.TextTestRunner().run(suite)
我真的不太了解单元测试在做什么,但也不太明白。
假设您的所有代码都在
main.py
def format_person_info(h, a, b= None):
if b != None:
a = ("{} , {} , {}".format(h, b, a))
else:
a = ("{} , {}".format(h, a))
return a
您可以为此方法运行单元测试,如下所示:
tests.py
当您运行测试时,输出将如下所示:
import main
import unittest
class hab_Test_Class(unittest.TestCase):
def test_pass2(self):
return_value = main.format_person_info("shovon","ar")
self.assertIsInstance(return_value, str, "The return type is not string")
self.assertEqual(return_value, "shovon , ar", "The return value does not match for 2 parameters")
def test_pass3(self):
return_value = main.format_person_info("shovon","ar",18)
self.assertIsInstance(return_value, str, "The return type is not string")
self.assertEqual(return_value, "shovon , 18 , ar", "The return value does not match for 3 parameters")
# i will use code below to run the unit test
t = hab_Test_Class()
suite = unittest.TestLoader().loadTestsFromModule(t)
unittest.TextTestRunner().run(suite)
现在让我们看看我们做了什么。我们使用
..
----------------------------------------------------------------------
Ran 2 tests in 0.016s
OK
检查返回类型是否为我们预期的字符串,并使用
assertIsInstance
检查两个和三个参数的输出。您可以通过 assertEqual
使用一组有效和无效的测试来进行测试。官方文档单元测试框架中有关于unittest的简要描述。
Nub团队连接安卓 7d&af_cost_currency 你非常喜欢公式 但他们买 90867856×74*8.0*