Now the unit tests are getting regular too.

Make a python reference to the following post,

http://www.cnblogs.com/sunshine-blog/p/6735690.html

I’ll end up using Django and mocks.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~



Python UnitTest consists of four components:

  • Test fixture — the environment in which your test code runs. It refers to the work to be done before and after the test is prepared, including setUp() and tearDown().
  • TestCase — the base class for all test cases, which is the most basic building block in software testing;
  • TestSuite — a collection of test cases;
  • Test runner — Execution of test cases

Note1: what is a test case? Answer: A test case is a complete testing process, including setUp of test preparation environment (setUp), execution of test code (run), and tearDown of test environment (tearDown).

 

Unittest Test code flow:

  1. Write a TestCase
  2. Load TestCase from TestLoader to TestSuite
  3. TestSuite is then run by TextTestRunner, and the predictive results remain in TextTestResult (executed using the command line or the unitTest.main () call)



#! /usr/bin/env python # -*- coding: utf-8 -*- import unittest import myclass class mytest(unittest.TestCase): Def setUp(self): self.tclass = myclass.myclass() def tearDown(self): Def test_sum(self) def test_sum(self): Self. AssertEqual (self) tclass) sum (1, 2), Skip ("I don't want to run this case.") def test_sub(self): self.assertEqual(self.tclass.sub(4, 2), 2) def test_multi(self): self.assertEqual(self.tclass.multi(2, 3), 5) if __name__ == '__main__': Tests = [mytest("test_sum"),mytest("test_sub"),mytest("test_multi")]) tests = Unittest.testloader ().loadtestsfromtestcase (mytest) # TestSuite = unittest.testsuite () Log_name = "log.txt" with open(log_name, 'w+') as f: Result = unittest.textTestrunner (stream=f, verbosity=2).run(suite) TextTestRunner(verbosity=2).run(suite) print("testsRun:%s"% result.testsrun) print("failures:%s"%len(result.failures)) print("errors:%s"%len(result.errors)) print("skipped:%s"%len(result.skipped)) ''' unittest.main() '''Copy the code