使用If Else条件在Python中进行单元测试



假设我们有以下函数:

import unittest
def func_test():
indicator = True
if(indicator == True):
a = 5
b = 10
total = a+b
return total
else:
a = 6
b = 10
total = a*b
return total

class TestJob(unittest.TestCase)

total = func_test(indicator = True)
def test_func_test(self):
self.assertGreater(total, 0)

我们将如何测试单元测试的else条件?

您可以将indicator作为函数测试的参数。

def func_test(indicator=True):
if indicator:
...
else:
...

class TestJob(unittest.TestCase)

def test_func_test(self):
self.assertGreater(func_test(indicator=True), 0)
def test_func_test_else(self):
self.assertGreater(func_test(indicator=False), 0)

最新更新