Selenium-Unittest unit testing framework

1, Unittest Introduction

Why learn unit testing framework for
the organization and running of test cases need to participate in unit testing framework to meet the needs of different test scenarios, unit testing framework provides a rich comparison methods: comparison of actual results with the expected results of the test result ==
unit tests framework provides a rich log: given the number of test failures and the reasons and failed by a
common unit testing framework which
Doctest, unittest, pytest, nose and so on
Java - "junit (testng), C ++ -" googleTest, c # - "nunit

2, Unittest core components

 3, Unittest works

Testcase: a testcase is a test case. Setup (ready test environment), run (test execution), teardown (reduction test environment).
TestSuite (set or kit): a plurality of test cases is set suite, suite may comprise a plurality of test cases, it can also be nested suite.
TestLoader: testcase used to load into the testsuite.
TestTestRunner: Example performed with a test (run method), the test results are saved in the TestTestresult.
fixture: build and destroy a test environment.

 4, Unittest test tissue cells

Construction unit test
scenario: replace string Examples

 

 5, build test suites (optimization of test cases)

The increase in unit test cases, there will be many similar operations in the respective test code in consideration setUp (prepared test environment), the tearDown (reduction of the test environment)

 

 6, a plurality of test tissue (test optimization depth)

Continue to optimize the test: All test cases do not need to have a separate class for storage, the time consuming manner, to store the beginning of the test by a function test.

 

 7, build test suites

A plurality of test cases is set suite, suite may comprise a plurality of test cases, it can also be nested suite.

 8, test suite optimization

The operative part of the test case

 All test performed under a test class, unittest module provides a makesuite

 Control 9, a plurality of test classes

 

 10, a test is performed

First StringReplaceTestCase in a separate file, a py StringReplace.py
comments on the method, as the method description.
The operative part of the test cases, create a test_suite.py, user control measures to implement use cases
to run all the test cases.

11, the test is skipped

StringReplace modify files, understanding skip, skipif, skipunless

 12, Unittest framework summary

unittest.TestCase: TestCase class, all of the basic classes of test case class inheritance. the Test class (of unittest.TestCase):
unittest.main (): Use a unit can easily be changed to test module test scripts can be run directly
unittest.TestSuite (): unittest frame the TestSuite () class is used to create test suites of.
unittest.TextTestRunner (): unittest TextTestRunner frame (), a () method to run the test suite is assembled through the following class RUN, the parameters for the test suite suite.
unittest.skip (): decorator, when running with the embodiment, execution may not want some cases, be used to temporarily shield the decorator strip test. Common usage To debug a test, to be shielding other use cases
@ 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

setUp () method for initialization performed prior to the test. The test requires access to the browser, the browser can be instantiated in setUp drive.
tearDown (): tearDown () method is used to perform remedial work after the test, close the browser.
Assertions: during execution of the test case, whether performed by the final use case, 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.
addTest (): method is to add the test to the test suite.

 run (): is the test run the test suite, the suite of test suite parameters

 13, HTML report describes

HTMLTestRunner extension is a standard library unittest Python unit testing framework for generating a test report HTML
Download: http://tungwaiyip.info/software/HTMLTestRunner.html

 14, HTML test results

HTMLTestRunner.py Download http://tungwaiyip.info/software/HTMLTestRunner.html
storage path HTMLTestRunner.py is placed in C: \ the Users \ Administrator \ AppData \ Local \ Programs \ Python37 \ Lib
HTMLTestRunner.py is based python2 * the development, in order to be able to be used on python3, need to modify the file.
Review summary: 
line 94, to modify the import StringIO import io
, line 539, the self.outputBuffer = StringIO.StringIO () modified self.outputBuffer = io.StringIO ()
on line 642, will if not rmap.has_key ( cls): modified if not cls in rmap:
766th row, the uo = o.decode ( 'latin-1 ') modified uo = e
, line 772, the ue = e.decode ( 'latin-1 ') modified ue = e
, line 631, the print >> sys.stderr, '\ nTime Elapsed :% s'% (self.stopTime-self.startTime) modified to print (sys.stderr,' \ nTime Elapsed :% s '% (self.stopTime-self.startTime))

Test_suite HTML-based report file is generated, stored in the D drive called result.html

Guess you like

Origin www.cnblogs.com/katyhudson/p/12463325.html