Concept and automated testing unit test module -unittest Python

Basic concepts

Test Definition

Software Testing (English: Software Testing), describes a process for facilitating identification of software correctness, completeness, security and quality. The process of actual output compared with the expected output

 Test Category

Unit Test: The whole software is decomposed into each function , and then to test the units.

Integration Testing: Integration testing as opposed to unit testing, the principle is the part to be tested as part of an integrated whole, and then tested for such an integrated part.

Black Box Testing: The software test environment to simulate invisible "black box." Observation data output through the data input, the internal check software functionality.

White-box testing: internal software applications, source code and other products based on the internal work process debugging.

Assertions defined

When you write code, we will always make some assumptions, assertions is used to capture these assumptions in your code , you can assert regarded as an advanced form of exception handling. Some assert expressed as a Boolean expression, programmers believe a particular point in the program of the expression is true. You can enable and disable assertions at any time to verify, so you can enable assertions at the time of testing, but to disable assertions at the time of deployment. Likewise, the program put into operation, the end user can re-enable assertions when they encounter problems.

 

Python unit test module -unittest

Examples assertion

a = 0
assert a,"a is false"
print(a)

The above assertion code is similar to the following if statement

Assertion of superiority

1. code defensively

2. Notes played a special role

Common Glossary

TestCase (test):  base class for all test cases, which is the basic software testing constituent unit. A test case is a test case, is a complete testing process, including test environment before setUp build, test code execution (run), and the reducing environment of the test (tearDown). Test test is a complete unit, can be verified on a particular issue.

TestSuite (test suite) : is a collection of a plurality of test cases test case TestSuite, can be nested TestSuite TestSuite

TestLoder: is used to load into TestSuite TestCase, where there are several loadTestsFrom_ () method, that is, from various places to find TestCase, create instances of them, and then add to TestSuite, and then returns an instance of TestSuite

TextTestRunner : the test is performed, wherein the run (test) performs TestSuite / TestCase the run (result) method.

TextTestResult : The test results are saved to TextTestResult instance, how many use cases including running, how much success and failure information

TestFixture : also known as scaffold test, test code operating environment, refer to before and after the implementation of test preparation work to be done, including the setUp and tearDown methods

unittest exemplary method using

import unittest
#unittest使用的方法
class OurTest(unittest.TestCase):
    """
    继承编写测试的基础类
    """
    def setUp(self):
        """
        类似于类的init方法,在测试执行之初制动执行,通常用来做测试数据的准备
        """
    def test_add(self):
        """
        具体测试的方法,使用testcase编写具体测试的方法,函数名称必须以test开头
        函数当中的内容通常是获取预期值,和运行结果值
        然后对两个值进行断言
        """
    def tearDown(self):
        """
        类似类的del方法,用来回收测试的环境
        """

if __name__ == "__main__":
    unittest.main()

 

 

 

import unittest


class OurTest(unittest.TestCase):

    def setUp(self):

        self.a = 1 #测试使用的参数1
        self.b = 1 #测试使用的参数2
        self.result = 3 #预期的结果
    def test_add(self):
        run_result = self.a + self.b
        self.assertEqual(run_result,self.result,"self.a+self.b不等于3") #断言两个值相等
    def tearDown(self):

if __name__ == "__main__":
    unittest.main()

 

 

Unitest common assertion methods

Assertion assertion methods described

assertEqual (arg1, arg2, msg = None) to verify arg1 = arg2

assertNotEqual (arg1, arg2, msg = None) to verify arg1! = arg2

assertTrue (expr, msg = None) to verify expr is true, if false

assertFalse (expr, msg = None) to verify expr is false, if true

assertIs (arg1, arg2, msg = None) to verify arg1, arg2 same object

assertIsNot (arg1, arg2, msg = None) to verify arg1, arg2 not the same object

assertIsNone (expr, msg = None) is verified expr None

assertIsNotNone (expr, msg = None) to verify expr not None

assertIn (arg1, arg2, msg = None) to verify arg1 arg2 is a substring of

assertNotIn (arg1, arg2, msg = None) is not verified sub arg1 arg2 string

assertIsInstance (obj, cls, msg = None) to verify obj is an instance of cls

Examples ssertNotIsInstance (obj, cls, msg = None) to verify the obj is not cls

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_39112101/article/details/94723856