import unittest from test_temp2 import TestUnit if __name__ == '__main__': suite = unittest.TestSuite() tests = [TestUnit("test_add"), TestUnit("test_minus"), TestUnit("test_divide")] suite.addTests(tests) runner = unittest.TextTestRunner(verbosity=2) runner.run(suite)
import unittest from spam2 import * class TestUnit(unittest.TestCase): """unittest test class of spam2""" def setUp(self): print("do something before test") def tearDown(self): print("do something after test") @unittest.skip("i don't wanna test it") def test_add(self): """test method add(a, b)""" self.assertEqual(3, add(1, 2)) self.assertNotEqual(3, add(2, 2)) def test_minus(self): """test method minus(a, b)""" self.assertEqual(1, minus(3, 2)) self.assertNotEqual(1, minus(3, 1)) def test_multi(self): """test method multi(a, b)""" self.assertEqual(6, multi(2, 3)) def test_divide(self): """test method divide(a, b)""" self.assertEqual(2, divide(6, 3)) print(5.0/2) self.assertEqual(2.5, divide(5, 2)) if __name__ == '__main__': unittest.main()
def add(a, b): return a + b def minus(a, b): return a - b def multi(a, b): return a * b def divide(a, b): return a / b