实例介绍
【实例简介】Python 单元测试
【实例截图】
【核心代码】
Contents About the Author .............................................................................. xi About the Technical Reviewers ...................................................... xiii Acknowledgments........................................................................... xv Introduction................................................................................... xvii ■Chapter 1: Introduction to Python .................................................. 1 The History of Python .............................................................................. 1 Features of Python .................................................................................. 2 Simple.......................................................................................................................2 Easy to Learn............................................................................................................2 Easy to Read.............................................................................................................3 Easy to Maintain .......................................................................................................3 Open Source .............................................................................................................3 High-Level Language................................................................................................3 Portable ....................................................................................................................3 Interpreted................................................................................................................3 Object-Oriented ........................................................................................................4 Extensible .................................................................................................................4 Extensive Libraries ...................................................................................................4 Robust ......................................................................................................................4 Rapid Prototyping .....................................................................................................4 Memory Management ..............................................................................................5 Powerful ...................................................................................................................5 Community Support..................................................................................................5 www.allitebooks.com ■ CONTENTS vi Python 3 .................................................................................................. 5 The Differences Between Python 2 and Python 3..................................................... 5 Why Use Python 3.....................................................................................................6 Installation of Python 3............................................................................ 7 Installation on Linux .................................................................................................7 Installation on MacOS X............................................................................................8 Installation on Windows............................................................................................8 Running a Python Program and Python Modes..................................... 11 Interactive Mode.....................................................................................................11 Normal Mode ..........................................................................................................12 IDEs for Python...................................................................................... 12 IDLE ........................................................................................................................13 The PyDev Plugin for Eclipse ..................................................................................14 Geany......................................................................................................................14 PyCharm.................................................................................................................15 Conclusion............................................................................................. 17 ■Chapter 2: Getting Started............................................................ 19 A Brief Introduction to Software Testing Concepts................................ 19 Unit Testing.............................................................................................................19 Test Automation......................................................................................................19 Using Docstrings ................................................................................... 20 Example of a Docstring in Python...........................................................................21 A Brief Introduction to doctest .............................................................. 24 Failing Tests............................................................................................................26 Separate Test File ...................................................................................................27 Advantages and Disadvantages of doctest............................................................. 28 Conclusion............................................................................................. 29 www.allitebooks.com ■ CONTENTS vii ■Chapter 3: Unittest........................................................................ 31 Introduction to xUnit.............................................................................. 31 Using Unittest........................................................................................ 32 Order of Execution of the Test Methods.................................................................. 34 Verbosity Control ....................................................................................................35 Multiple Test Classes Within the Same Test File/Module........................................ 36 Test Fixtures ...........................................................................................................37 Running Without unittest.main().............................................................................39 Controlling the Granularity of Test Execution.......................................................... 40 Listing All the Command-Line Options and Help ....................................................42 Important Command-Line Options..........................................................................43 Creating a Test Package .........................................................................................46 Organizing the Code ...............................................................................................48 Test Discovery ....................................................................................... 53 Coding Conventions for unittest............................................................ 54 Assertions in unittest ............................................................................ 55 Other Useful Methods............................................................................ 56 Failing a Test ......................................................................................... 57 Exceptions in the Test Case ....................................................................................59 assertRaises().........................................................................................................60 Conclusion............................................................................................. 63 ■Chapter 4: nose and nose2........................................................... 65 Introduction to nose .............................................................................. 65 Installing nose on Linux OS ....................................................................................65 Installing nose on MacOS and Windows.................................................................66 Verifying the Installation.........................................................................................66 Getting Started with nose.......................................................................................66 A Simple nose Test Case.........................................................................................66 www.allitebooks.com ■ CONTENTS viii Running the Test Module with nosetests................................................................67 Getting Help............................................................................................................68 Organizing the test code.........................................................................................68 Test Discovery ........................................................................................................69 Fixtures for Classes, Modules, and Methods......................................... 70 Fixtures for Functions.............................................................................................72 Fixtures for Packages.............................................................................................74 Alternate Names of the nose Fixtures .................................................................... 75 assert_equals().......................................................................................................75 Testing Tools.......................................................................................... 77 ok_ and eq_............................................................................................................77 The @raises() Decorator.........................................................................................78 The @timed() decorator..........................................................................................79 Report Generation ................................................................................. 80 Creating an XML Report..........................................................................................80 Creating an HTML Report........................................................................................ 81 Creating Color Output in the Console......................................................................82 Running unittest Tests from nose.......................................................... 83 Advantages of nose over unittest.......................................................... 83 Disadvantages of nose .......................................................................... 84 Using Nose 2 ......................................................................................... 84 Conclusion............................................................................................. 85 ■Chapter 5: pytest.......................................................................... 87 Introduction to pytest ............................................................................ 87 Simple Test .............................................................................................................88 Running Tests with the py.test Command............................................................... 89 Test Class and Test Package in pytest.................................................................... 90 Test Discovery in pytest..........................................................................................91 www.allitebooks.com ■ CONTENTS ix xUnit-Style Fixtures ................................................................................................91 pytest Support for unittest and nose ...................................................................... 93 Introduction to pytest Fixtures .............................................................. 93 Scope of pytest Fixtures .........................................................................................96 pytest.raises().........................................................................................................97 Important pytest Command-Line Options ............................................. 98 Help ........................................................................................................................98 Stopping After the First (or N) Failures ...................................................................98 Profiling Test Execution Duration............................................................................99 JUnit-Style Logs .....................................................................................................99 Generating a Plain Result .......................................................................................99 Sending a Test Report to Online pastebin Service.................................................. 99 Conclusion............................................................................................. 99 ■Chapter 6: Tips and Tricks.......................................................... 101 Coding and Filenaming Conventions for Easier Test Discovery........... 101 Test-Driven Development with pytest ................................................. 102 Conclusion........................................................................................... 108 Index.............................................................................................. 109
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论