Pyunit Test Framework

Source: Internet
Author: User

I. Overview

The main problem in this series is "interface Automation Test", the selected test language is the Python scripting language. So far, Python is recognized as one of the best languages for automation applications

Second, Pyunit testing framework

using Python as an automated programming language, it is natural to use Pyunit as an automated testing framework. the test framework UnitTest to achieve the following objectives:

Support Automated Testing

Have all test scripts share the open (Setup) and shutdown (shutdown) code

Test case scripts can be organized in a collection (collections) way

Separate all test scripts from the test report framework

To achieve these goals, UnitTest supports several key concepts:

Testing Device (Test fixture):

Do some preparatory work for one or more test cases, such as connecting a database, creating a directory, or opening a process

Test Case:

Test cases are the smallest unit of test behavior and are tested by comparing some input and output values

Test suite:

Aggregates a collection of test cases or test case collections that are organized together. All test cases in a test suite can be executed in bulk

Test Actuator (runner):

The organization arranges the components of the test script execution activity. The test executor displays the results of the test script through some graphical interfaces, a text interface, or some special values to return. Primarily used to generate test reports

Iii. Basic Examples

The following example also comes from the official document basic_demo.py:

1# coding:utf-8"""2 Basic Automated test script basic_demo.py3 """__author__ = ' Zheng ' import unittestclass teststringmethods (unittest. TestCase):4 5 def setUp (self):6Print'init by setUp ...'7 8 def tearDown (self):9Print'end by TearDown ...'Ten  One def test_upper (self): ASelf.assertequal ('Foo'. Upper (),'FOO') -  - def test_isupper (self): theSelf.asserttrue ('FOO'. Isupper ()) -Self.assertfalse ('Foo'. Isupper ()) -Self.asserttrue ('Foo'. Isupper ()) -  + def test_split (self): -s ='Hello World' +Self.assertequal (S.split (), ['Hello',' World']) A# Check that S.split fails when the separator isNot astring at With self.assertraises (TypeError): -S.split (2)if__name__ = ='__main__': -Unittest.main ()

The test case scripts are organized in the following ways:

Independent test function

Single use Case test class

Multi-use Case test class

One of the many use-case test classes, which is the basic example above, has the following characteristics:

1. The test class inherits from the UnitTest. TestCase

2. A test class can manage multiple test script functions

3. Test script function names need to start with test_

4. All the test functions in a test class share the setup and teardown functions

Run this program in the console:

1 ? src git: (master)? Python basic_demo.py2 init by setUp ...3 fend by TearDown ...4 init by setUp ...5 end by TearDown ...6 . Init by setUp ...7 end by TearDown ...8. ======================================================================fail:test_isupper (__main__. Teststringmethods)----------------------------------------------------------------------9 Traceback (most recent):TenFile"basic_demo.py", line -,inchTest_isupper OneSelf.asserttrue ('Foo'. Isupper ()) Assertionerror:false isNottrue---------------------------------------------------------------------- ARan3Testsinch 0. 001s -  -FAILED (failures=1)? src git: (master)?

Through the above results, it is not difficult to find the following useful information:

Test the order in which the scripts are executed, Setup then execute the test function, and then teardown

The test script output prints out the script that was not passed by the test and the problems encountered, which is a result report that is useful to testers

The total number of test scripts executed and the time spent in testing

Iv. Summary

  This article's basic example of the main function takes the simplest way to run all of the test cases directly and generate a default text report. In fact, we just need to make some simple changes to the calling function, which can organize the test cases reasonably and obtain the useful data information, so as to integrate with the information system, and form a better extension

Pyunit Test Framework

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.