I don't test python advanced programming

Source: Internet
Author: User
Tags integer division

#-*-Coding:utf-8-*-
__author__ = ' Administrator '
#测试驱动开发 (Learn from Advanced programming, Chapter 11th)
#测试驱动开发也叫TDD, a simple technique for producing high-quality software, is widely used in the Python community and more in static languages.
#我不测试
"""
Test-driven Development principle: Test before code writing test
For example, if a developer wants to write a mathematical mean function that calculates a series of values, it will first write a few instances to get the desired effect, as follows
"""
#assert average (==2)
#assert average (1,-3) ==-2
#提供以下函数, to implement this method
def average (*num):
return sum (num)/len (num)
Assert average (==2)
Assert average (1,-3) ==-1
#a: Defects or unintended results can result in the following problems
#assert average (0,1) ==.5 Assertionerror
#出现a问题时, you can change the code accordingly, as follows
def avg (*a):
Num=[float (a) for a in a]
return sum (num)/len (num)
Assert avg (0,1) ==.5
#更多测试
# try:
# AVG ()
# except TypeError as S:zerodivisionerror:integer division or modulo by zero
# #出现一个异常
# Pass
def avg1 (*a):
If a== ():
Raise TypeError (' You need-provide at '
' Least one num '))
Num=[float (a) for a in a]
return sum (num)/len (num)
Try
AVG1 ()
Except TypeError as S:
Pass
#然后我们可以到指定的函数里面进行收集测试, at each run, make the following changes:
def test ():
Assert Avg1 (==2)
Assert Avg1 (1,-3) ==-1
Assert Avg1 (0,1) ==.5
Try
AVG1 ()
Except TypeError:
Pass

Test ()
#每次修改时, Test () and AVG1 () will be modified together, and each run to confirm that all tests are still valid, what testing packages are placed in the tests folder, where each module can have a corresponding test module
#以下几个好处4个:
#a避免软件退化
#退化的原因: Because there is no way to guess the results of a change in the code base at a certain point, modifying some code might break the others, and have horrible side effects, such as damaging the data
#解决方法: The software is tested every time it is modified
#将代码库开发给多个开发人员将会放大这个问题, because everyone is not fully aware of all development activities, and although version control systems can avoid this problem, they cannot avoid all unnecessary interactions
#TDD有助于减少软件退化, the entire software can be automatically tested after each modification, as long as each function has the right set of tests to do this, and when TDD is really implemented, the test library and code base grow together
#因为完整的测试可能相当长的时间, so delegating it to Buidbot is a good practice, it can be done in the background
#b增进代码
"""
When writing a new module, function, or class, the developer is concerned with how to write p-x and how to generate the best code, but when he focuses on the algorithm, he may forget to look at the user's thoughts: when and how his functions are used, is the parameters simple and logical, and is the API name correct?
Using the skills of previous studies, such as choosing a good name, can make it efficient, and the method is to test, which is the time when the developer realizes that the code is logical and easy to use, often the first refactoring occurs immediately after the module, class, or function is complete.
When writing tests, which is a specific usage scenario for the code, there is a point of view for acquiring such a user, so that developers can often use TDD to generate orphan code
It is difficult to test the computational complexity with side-effect functions, and the test-oriented code should be written with a clearer and more modular structure.
"""
#提供最佳的开发人员文档
"""
Testing is the best way for developers to learn how software works, primarily to read them for use scenarios, to build fast and in-depth observations of how the code works, and sometimes an example value of 1000 words
Tests that are always updated with the code base, can generate the software deuterium can have the best developer documentation, the test will not be as static as the document, otherwise it will fail
"""
#更快地产生健壮的代码
"""
Not testing will result in a long debugging time, a partial flaw in the software may be found in another part of the "irrelevant", the test failure is a better time to deal with small defects
Because you can get tips to understand what the problem really is, and testing is often more interesting than debugging because it's working
"""
# #哪一类测试
#2个测试
#1: Acceptance Test (also known as functional test)
"""
Written outside of a cycle to verify that the application meets the requirements, generally like a checklist for software, but this is not done by TDD and needs to be done by a manager or a customer (concept)
"""
#测试工具有几个如下
"""
Application Selenium (Web UI for JavaScript)
Web application Zope.testbrowser (do not test JS)
WSGI application Paste.test.fixture (do not test JS)
Gnome Desktop App Dogtail
Win32 Desktop Application Pywinauto

For a list of more functional test tools, visit the following page:(because some sites may not be accessible)
"""
#2: Unit Testing
"""
is a very suitable low-level test for the TDD approach, which focuses on a single module and provides testing for it, stubborn does not involve any other modules. The test separates the module from the rest of the application, and when an external item is required (such as a database), it is replaced by a simulation or a mock object.
Python Standard test Tool
A1:unittest (Https://docs.python.org/2.7/library/unittest.html?highlight=unittest#module-unittest)
Originally written by Steve Purcell (formerly the People Pyunit)
A2:doctest (https://docs.python.org/2.7/library/doctest.html?highlight=doctest#module-doctest) a Word test tool
"""
#a1: Its functionality is the same as what JUnit does with Java, providing a set of methods called the Testcae base class that validates the invocation output, as follows:
Import UnitTest as T
Class T1 (T.testcase):
def test_avg (self):
Self.assertequals (Avg1 (), 2)
Self.assertequals (AVG1 (1,-3),-1)
Self.assertequals (AVG1 (0,1),. 5)
Self.assertraises (TYPEERROR,AVG1)

#t. Main ()
#main函数是扫描上下文并查找从T1继承类, instantiate these classes, and then run
#如果av1在xx. py module, then the test class is called UT and written in the specified. py file
From unils import avg1
Import UnitTest
Class T1 (UnitTest. TestCase):
def test_avg (self):
Self.assertequals (Avg1 (), 2)
Self.assertequals (AVG1 (1,-3),-1)
Self.assertequals (AVG1 (0,1),. 5)
Self.assertraises (TYPEERROR,AVG1)
If __name__== ' __main__ ':
Unittest.main ()
#这个例子进行返回在命令行下调用于__main__部分或者为测试运行程序所用的TS实例, this script builds a test activity outside of all the test modules
Class T1 (UnitTest. TestCase):
def test_avg (self):
Self.assertequals (Avg1 (), 2)
Self.assertequals (AVG1 (1,-3),-1)
Self.assertequals (AVG1 (0,1),. 5)
Self.assertraises (TYPEERROR,AVG1)
Class T2 (UnitTest. TestCase):
def test_another_thes (self):
Pass
Def test_suite ():
def suite (t):
return Unittest.makesuite (t)
Suite=unittest. TestSuite ()
Suite.addtest ((S (T1), Suite (T2)))
Return Suite
If __name__== ' __main__ ':
Unittest.main (defaulttest= ' test_suite ')
#一般来说, all tests can be done with a global script that will find and run tests on the code tree called Test discovery
#doctest: is a module used in an interactive command line session to extract versions from a document file or DocString, and to replay them to check that the output of the finalized instance is the same as the actual output, as in the following code
Import Doctest
Print doctest.testfile (' Test.tex ', verbose=true)
#使用doctest有什么好处:
"""
Packages can create documents and tests from the sample
Document samples are always up-to-date
Use the example in Doctest to write a package
But do not use doctest to expire unit tests, it should only be used to provide human readable examples in the documentation, that is to say: When lucidity is concerned about low-level transactions or complex test devices that can clutter the document, do not

"""

I don't test python advanced programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.