python-unittest unittest unit testing framework summary

unittest unit testing framework summary

    unittest unit test frame may be applicable not only to the test unit, can be applied WEB automated test development and execution of the test frame may be organized to execute test cases, and provides a rich assertions, by determining whether the test case, the test results to generate the final . Today I will summarize how to use unittest Unit testing frameworks for automated testing WEB.

table of Contents

First, the various properties described unittest module

Second, the idea of ​​using the unittest framework to write test cases

Third, the use unittest framework to write test cases examples

 

First, the various properties described unittest module

Click Back to Contents

    First to chat with each attribute unittest module, the so-called know ourselves before being victorious, unittest understand the various attributes of, for example subsequent written a great help.

1.unittest following properties:

['BaseTestSuite', 'FunctionTestCase', 'SkipTest', 'TestCase', 'TestLoader', 'TestProgram', 'TestResult', 'TestSuite', 'TextTestResult', 'TextTestRunner', '_TextTestResult', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__unittest', 'case', 'defaultTestLoader', 'expectedFailure', 'findTestCases', 'getTestCaseNames', 'installHandler', 'loader', 'main', 'makeSuite', 'registerResult', 'removeHandler', 'removeResult', 'result', 'runner', 'signals', 'skip', 'skipIf', 'skipUnless', 'suite', 'util']

Description:

unittest.TestCase: TestCase class, all of the basic classes of test case class inheritance.

class BaiduTest(unittest.TestCase):

unittest.main (): Use she can easily be changed to a module unit test test scripts can be run directly, main () method uses the class to search all TestLoader test methods contained in the module in order to "test" the beginning of the name, and automatically execute them. The default order of execution of the method is: according to the test load ASCII code order, the sequence of numbers and letters: 0-9, AZ, az. Therefore, in the beginning of the test method A will be preferentially executed, after a beginning to be performed.

unittest.TestSuite (): unittest framework TestSuite () class is used to create test suites.

unittest.TextTextRunner (): unittest frame TextTextRunner (), a () method to run the test suite is assembled through the following class RUN, the parameters for the test suite suite.

unittest.defaultTestLoader (): defaultTestLoader () class by class below discover () method can automatically catalog start_dir more test matches to find a test case file (test * .py), and find the test assembly to test suite , can be performed directly discover run () method. Usage is as follows:

discover=unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py')

unittest.skip (): decorator, when operated with cases, with some cases might not want the like, can be used temporarily shield decorative strip test. A common use of this is for example want to debug a test case, would like to shield other use cases can be shielded with a decorator.

@ Unittest.skip (reason): skip (reason) decorator: decorative unconditional skip test, and the test is skipped reasons.

@ Unittest.skipIf (reason): skipIf (condition, reason) decorators: the condition is true, the test is skipped decoration, and the reasons for skipping testing.

@ Unittest.skipUnless (reason): skipUnless (condition, reason) decorator: the condition is false, skip the decoration of the test, and the reasons for skipping tests.

@ Unittest.expectedFailure (): expectedFailure () test is marked as failed.

 

2.TestCase class attributes are as follows:

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addSkip', '_baseAssertEqual', '_classSetupFailed', '_deprecate', '_diffThreshold', '_formatMessage', '_getAssertEqualityFunc', '_truncateMessage', 'addCleanup', 'addTypeEqualityFunc', 'assertAlmostEqual', 'assertAlmostEquals', 'assertDictContainsSubset', 'assertDictEqual', 'assertEqual', 'assertEquals', 'assertFalse', 'assertGreater', 'assertGreaterEqual', 'assertIn', 'assertIs', 'assertIsInstance', 'assertIsNone', 'assertIsNot', 'assertIsNotNone', 'assertItemsEqual', 'assertLess', 'assertLessEqual', 'assertListEqual', 'assertMultiLineEqual', 'assertNotAlmostEqual', 'assertNotAlmostEquals', 'assertNotEqual', 'assertNotEquals', 'assertNotIn', 'assertNotIsInstance', 'assertNotRegexpMatches', 'assertRaises', 'assertRaisesRegexp', 'assertRegexpMatches', 'assertSequenceEqual', 'assertSetEqual', 'assertTrue', 'assertTupleEqual', 'assert_', 'countTestCases', 'debug', 'defaultTestResult', 'doCleanups', 'fail', 'failIf', 'failIfAlmostEqual', 'failIfEqual', 'failUnless', 'failUnlessAlmostEqual', 'failUnlessEqual', 'failUnlessRaises', 'failureException', 'id', 'longMessage', 'maxDiff', 'run', 'setUp', 'setUpClass', 'shortDescription', 'skipTest', 'tearDown', 'tearDownClass']

Description:

setUp (): setUp () method for initialization performed prior to the test. As a test case needs to access the database, the database connection can be established in setUp and initialized. As test cases need to log web, it may be the first instance of the browser.

tearDown (): tearDown () method is used after the aftermath of test case execution. The close the connection. Close the browser.

assert * (): number of assertions: during execution of the test case, if the embodiment is performed by the final use, equality is determined by determining the actual and the expected test results obtained.

assertEqual (a, b, [msg = 'print the test failure information']): assert a and b are equal, are equal by test cases.

assertNotEqual (a, b, [msg = 'print the test failure information']): assert a and b are equal, not equal, then through the test.

assertTrue (x, [msg = 'test fails printed information']): True if x is asserted, then the test is True through.

assertFalse (x, [msg = 'test fails printed information']): assert x is False, False if the test is passed.

assertIs (a, b, [msg = 'test fails printed information']): assert whether a b, then the test is passed.

assertNotIs (a, b, [msg = 'test fails printed information']): assert whether a b, then the test is not passed.

assertIsNone (x, [msg = 'test fails printed information']): x is asserted None, None of the test is passed.

assertIsNotNone (x, [msg = 'test fails printed information']): x is asserted None, None of the test is not passed.

assertIn (a, b, [msg = 'test fails printed information']): b is in the asserted a, b, in the test passed.

assertNotIn (a, b, [msg = 'test fails printed information']): b is in the asserted a, b, the test is not passed.

assertIsInstance (a, b, [msg = 'print the test failure information']): assert a is b is an example, and the test is passed.

assertNotIsInstance (a, b, [msg = 'print the test failure information']): assert a is b is an example, and not by the test.

 

3.TestSuite class attributes are as follows :( Example needed when tissues)

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addClassOrModuleLevelException', '_get_previous_module', '_handleClassSetUp', '_handleModuleFixture', '_handleModuleTearDown', '_tearDownPreviousClass', '_tests', 'addTest', 'addTests', 'countTestCases', 'debug', 'run']

Description:

addTest (): addTest () method is to add the test suite to test, the following side, is added under BaiduTest test_baidu test under the category test_baidu module to the test suite.

suite = unittest.TestSuite()
suite.addTest(test_baidu.BaiduTest('test_baidu'))
 

4.TextTextRunner following properties needed when tissues :( Example)

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_makeResult', 'buffer', 'descriptions', 'failfast', 'resultclass', 'run', 'stream', 'verbosity']

Description:

run (): run () method is the test run the test suite, the suite is the reference test suite.

runner = unittest.TextTestRunner()
runner.run(suite)

Guess you like

Origin www.cnblogs.com/xiaohuihui92/p/11691139.html