任何人请帮助我为什么不能在shell中看到测试的输出结果。随函附上模块代码和测试文件,它们都保存在目录中。非常感谢,有谁能帮我编写一本适合初学者使用的python测试教材呢?
我写信给shell: python calculate_test.py -v
calculate.py:这应该是模块文件
class Calculate(object):
def add(self, x, y):
return x + y
if __name__ == '__main__':
calc = Calculate()
result = calc.add(2, 2)
print resultcalculate_test.py:,这是测试文件
import unittest
from app.calculate import Calculate #this is maybe the problem
class TestCalculate(unittest.TestCase):
def setUp(self):
self.calc = Calculate()
def test_add_method_returns_correct_result(self):
self.assertEqual("HelloWorld", self.calc.add("Hello", "World"))发布于 2020-03-20 17:27:13
测试文件的外观:
import unittest
from calculate import Calculate #calculate is file-module, method from module
class TestCalculate(unittest.TestCase):
def setUp(self):
self.calc = Calculate()
def test_add_method_returns_correct_result(self): #testcase1
self.assertEqual("HelloWorld", self.calc.add("Hello", "World"))
def test_correct_result(self): #testcase2
self.assertEqual(5, self.calc.add(2,2))
if __name__ == '__main__':
unittest.main()若要在命令行运行测试,请执行以下操作:
python -m unittest test_calculate.TestCalculatehttps://stackoverflow.com/questions/60774308
复制相似问题