Python 单元测试
ContentsAbout the Author .............................................................................. xiAbout the Technical Reviewers ...................................................... xiiiAcknowledgments........................................................................... xvIntroduction................................................................................... xvii■Chapter 1: Introduction to Python .................................................. 1The History of Python .............................................................................. 1Features of Python .................................................................................. 2Simple.......................................................................................................................2Easy to Learn............................................................................................................2Easy to Read.............................................................................................................3Easy to Maintain .......................................................................................................3Open Source .............................................................................................................3High-Level Language................................................................................................3Portable ....................................................................................................................3Interpreted................................................................................................................3Object-Oriented ........................................................................................................4Extensible .................................................................................................................4Extensive Libraries ...................................................................................................4Robust ......................................................................................................................4Rapid Prototyping .....................................................................................................4Memory Management ..............................................................................................5Powerful ...................................................................................................................5Community Support..................................................................................................5www.allitebooks.com■ CONTENTSviPython 3 .................................................................................................. 5The Differences Between Python 2 and Python 3..................................................... 5Why Use Python 3.....................................................................................................6Installation of Python 3............................................................................ 7Installation on Linux .................................................................................................7Installation on MacOS X............................................................................................8Installation on Windows............................................................................................8Running a Python Program and Python Modes..................................... 11Interactive Mode.....................................................................................................11Normal Mode ..........................................................................................................12IDEs for Python...................................................................................... 12IDLE ........................................................................................................................13The PyDev Plugin for Eclipse ..................................................................................14Geany......................................................................................................................14PyCharm.................................................................................................................15Conclusion............................................................................................. 17■Chapter 2: Getting Started............................................................ 19A Brief Introduction to Software Testing Concepts................................ 19Unit Testing.............................................................................................................19Test Automation......................................................................................................19Using Docstrings ................................................................................... 20Example of a Docstring in Python...........................................................................21A Brief Introduction to doctest .............................................................. 24Failing Tests............................................................................................................26Separate Test File ...................................................................................................27Advantages and Disadvantages of doctest............................................................. 28Conclusion............................................................................................. 29www.allitebooks.com■ CONTENTSvii■Chapter 3: Unittest........................................................................ 31Introduction to xUnit.............................................................................. 31Using Unittest........................................................................................ 32Order of Execution of the Test Methods.................................................................. 34Verbosity Control ....................................................................................................35Multiple Test Classes Within the Same Test File/Module........................................ 36Test Fixtures ...........................................................................................................37Running Without unittest.main().............................................................................39Controlling the Granularity of Test Execution.......................................................... 40Listing All the Command-Line Options and Help ....................................................42Important Command-Line Options..........................................................................43Creating a Test Package .........................................................................................46Organizing the Code ...............................................................................................48Test Discovery ....................................................................................... 53Coding Conventions for unittest............................................................ 54Assertions in unittest ............................................................................ 55Other Useful Methods............................................................................ 56Failing a Test ......................................................................................... 57Exceptions in the Test Case ....................................................................................59assertRaises().........................................................................................................60Conclusion............................................................................................. 63■Chapter 4: nose and nose2........................................................... 65Introduction to nose .............................................................................. 65Installing nose on Linux OS ....................................................................................65Installing nose on MacOS and Windows.................................................................66Verifying the Installation.........................................................................................66Getting Started with nose.......................................................................................66A Simple nose Test Case.........................................................................................66www.allitebooks.com■ CONTENTSviiiRunning the Test Module with nosetests................................................................67Getting Help............................................................................................................68Organizing the test code.........................................................................................68Test Discovery ........................................................................................................69Fixtures for Classes, Modules, and Methods......................................... 70Fixtures for Functions.............................................................................................72Fixtures for Packages.............................................................................................74Alternate Names of the nose Fixtures .................................................................... 75assert_equals().......................................................................................................75Testing Tools.......................................................................................... 77ok_ and eq_............................................................................................................77The @raises() Decorator.........................................................................................78The @timed() decorator..........................................................................................79Report Generation ................................................................................. 80Creating an XML Report..........................................................................................80Creating an HTML Report........................................................................................ 81Creating Color Output in the Console......................................................................82Running unittest Tests from nose.......................................................... 83Advantages of nose over unittest.......................................................... 83Disadvantages of nose .......................................................................... 84Using Nose 2 ......................................................................................... 84Conclusion............................................................................................. 85■Chapter 5: pytest.......................................................................... 87Introduction to pytest ............................................................................ 87Simple Test .............................................................................................................88Running Tests with the py.test Command............................................................... 89Test Class and Test Package in pytest.................................................................... 90Test Discovery in pytest..........................................................................................91www.allitebooks.com■ CONTENTSixxUnit-Style Fixtures ................................................................................................91pytest Support for unittest and nose ...................................................................... 93Introduction to pytest Fixtures .............................................................. 93Scope of pytest Fixtures .........................................................................................96pytest.raises().........................................................................................................97Important pytest Command-Line Options ............................................. 98Help ........................................................................................................................98Stopping After the First (or N) Failures ...................................................................98Profiling Test Execution Duration............................................................................99JUnit-Style Logs .....................................................................................................99Generating a Plain Result .......................................................................................99Sending a Test Report to Online pastebin Service.................................................. 99Conclusion............................................................................................. 99■Chapter 6: Tips and Tricks.......................................................... 101Coding and Filenaming Conventions for Easier Test Discovery........... 101Test-Driven Development with pytest ................................................. 102Conclusion........................................................................................... 108Index.............................................................................................. 109
评论