1
我想爲我的主文件calc.py
編寫測試,在模塊文件中使用unittest,MyTests.py
。在模塊中編寫單元測試的正確方法是什麼?
這裏是我主要的Python文件,calc.py
:
import myTests
def first(x):
return x**2
def second(x):
return x**3
def main():
one = first(5)
two = second(5)
if __name__ == "__main__":
main()
try:
myTests.unittest.main()
except SystemExit:
pass
這裏是我的MyTests.py
文件:
import unittest
import calc
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.testInput = 10
def test_first(self):
output = calc.first(self.testInput)
correct = 100
assert(output == correct)
def test_second(self):
output = calc.second(self.testInput)
correct = 1000
assert(output == correct)
運行我calc.py
,我得到下面的輸出:
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
爲什麼unittest打印我「Ran 測試「?
什麼是在模塊中編寫unittest的正確方法?