On Python Unit Testing

Significance test

  People for a specific problem, through analysis and design, and finally programming language to write a program, if it passed the language interpreter (compiler) checks can be run, then the next step is to try to make sure it does meet our needs. This article is to discuss how to confirm whether the program meets the needs of users.

Meet the demand, in other words normal function, confirm normal function can be confirmed from the following aspects:

  • Defined functions can return the correct results for all the right parameters
  • Written procedures can produce the correct output for all of the appropriate input

  Practice after quantization is through a series of trial runs to check the behavior of the program, input and output, if the inspection found the problem, correct, improve. This is also the original intention of functional testing and security testing.

Test Case

  The basic question to consider is how to run the test program, need to provide any data, various behaviors and conditions in order to maximize the inspection program, the maximum possible dug program errors and defects. Based on what the testing process designed to provide what the parameters of this inspection program running data set is known as a test case. A test case is quantifiable testing process.

Confirm test mode and distinguish between two types:

  • Black box
    just do not look at the code directly start to use the test program. Black Box is not discussed here

  • White box
    basis is to look at the internal structure of the white-box testing procedures (code) and the generated execution path may be selected by testing the internal structure embodiment, so that the program can be shown as many different behaviors in the trial run . The basic idea of this approach is: If all possible execution paths of (the order conditions, while, for, nested ... implementation structures) can give correct results, then the correctness of the program can be guaranteed.


Test function function Case

  All kinds of language will provide unit testing libraries, Python is no exception, python general use PyUnit (unittest) library, unittest Python comes with a unit testing framework used to write and run repeatable tests, here how to use unittest to test the use of the function, I am here simply with a few test methods, more test methods please refer to the official website ( https://docs.python.org/3/library/unittest.html ).

3 need to test the function:

def mysum(a, b):
    return a + b

def mysubtraction(a, b):
    return a - b

def is_evenNumbers(x):
    if (x % 2) == 0:
        return True
    else:
        return False

Test Method functions:

import unittest
import testbox.mymath as mymath


class Test(unittest.TestCase):
    def setUp(self):
        print("The unit test function start.")

    def test_mysum(self):
        self.assertEqual(mymath.mysum(1, 2), 3, "mysum function have error!")

    def test_mysubtraction(self):
        self.assertEqual(mymath.mysubtraction(2, 1), 1, "mysubtraction function have error!")

    def test_is_evenNumbers(self):
        self.assertTrue(mymath.is_evenNumbers(2), "error")

    def test_is_evenNumbers2(self):
        self.assertFalse(mymath.is_evenNumbers(3), "error")

    def tearDown(self):
        print("The unit test end.")

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

Output:

Testing started at 12:26 PM ...
The unit test function start.
The unit test end.
The unit test function start.
The unit test end.
The unit test function start.
The unit test end.
The unit test function start.
The unit test end.

Usage assert keyword

  In fact, the above functions and test functions usage is the same, but assert can be used directly in the code. This keyword is relatively uncommon, have not seen what the scene needs to use it, will be here for the case, I use it to write a demo.

def testasserts(a):
    assert a == 2, Exception("parameter a not is 2, so have a error.")
    if a == 2:
        print("function run.")
    print("OK. function end.")

if __name__ == '__main__':
    testasserts(1)
    print("Program is end.")

Output:

Traceback (most recent call last):
  File "/Users/Mysticbinary/Document/code/personage/python/TestPython1/testbox/testadd.py", line 9, in <module>
    testasserts(1)
  File "/Users/Mysticbinary/Document/code/personage/python/TestPython1/testbox/testadd.py", line 2, in testasserts
    assert a == 2, Exception("parameter a not is 2, so have a error.")
AssertionError: parameter a not is 2, so have a error.


Functional test case

  As a function of class functional testing and testing, but there is a trick, testing, use the class when they are required to instantiate the class and instance of the class action, can be placed setUp () operation inside.

We need to test the classes:

class Library:
    allBook = ["php", "java"]

    def __init__(self):
        print("Library class create completion.")

    def savebook(self, bookname):
        self.allBook.append(bookname);
        return self.allBook

    def showbook(self):
        print(self.allBook)
        return self.allBook

The test class method:

import unittest
import testbox.myclass as myc

class TestClass(unittest.TestCase):
    def setUp(self):
        print("The unit test class start.")
        self.library = myc.Library()
        self.newbook = "python"

    def test_savebook(self):
        self.assertIn(self.newbook, self.library.savebook(self.newbook), "errow 1")

    def test_showbook(self):
        self.assertIn(self.newbook, self.library.showbook(), "errow 2")

    def tearDown(self):
        print("The unit test end.")

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

Output:

Testing started at 12:31 PM ...
The unit test class start.
Library class create completion.
The unit test end.
The unit test class start.
Library class create completion.
['php', 'java', 'python']
The unit test end.


Safety test cases - SMS bombing

  As I said earlier, functional testing and safety testing have the same mind, but both the specific test techniques are not the same, but under certain scenarios, the use of unit testing methods, but also to test some of the security issues, such as ultra vires test interfaces, SMS text messaging interface to repeated bombing caused problems. I'm just here to start a discussion about examples do security testing through unit testing techniques, but to do in-depth research.

def send_message(phone):
    keys = phones_dict.keys()
    if phone not in keys:
        phones_dict[phone] = 0
    else:
        if phones_dict[phone] < 10:
            # 执行发短信的流程
            phones_dict[phone] = (phones_dict[phone] + 1)
            print("已经发送了{}次短信".format(phones_dict[phone]))
            return "success"
        else:
            print("抱歉,该{}手机号 已经达到今天发短信的上限,请明天再来。".format(phone))
            return "error"

Texting function test security test cases:

    def test_send_message(self):
        result = list()
        for i in range(0, 11):
            result.append(sms.send_message("13193388105"))
        print(result)
        self.assertNotIn("error", result, "send_message have error.")

Output:

Testing started at 9:48 PM ...
The unit test function start.
已经发送了1次短信
已经发送了2次短信
已经发送了3次短信
已经发送了4次短信
已经发送了5次短信
已经发送了6次短信
已经发送了7次短信
已经发送了8次短信
已经发送了9次短信
已经发送了10次短信
[None, 'success', 'success', 'success', 'success', 'success', 'success', 'success', 'success', 'success', 'success']
The unit test end.


__main__ global variable interpretation

  In addition to the test unit, set to run an inlet (module main ) is also a test mode, which is separate function calls for each module. __main__ is actually a global variable, the interpreter found that if the module is to be introduced, then __main__ will be assigned the name of the module, if this module is to start as the main module, the interpreter will give _ _main__ assigned the " main " string.


to sum up

  1. Not recommended in line with the program when it comes to assert, print debug statements and the like, to avoid information leakage.
  2. Unit testing is not only used only for functional testing, can also be used in some specific security test in (those specific scope, I did not study, if there is a demand, I may continue in-depth study).
  3. unittest assertion is very simple to use, basically at a glance to understand, but to understand the usage of assertions, does not mean you will write a test case, to see the structure of the program code execution is the key to write the test. Sentence is: the assertion is easy to write, easy to look at the code.

Guess you like

Origin www.cnblogs.com/mysticbinary/p/12099481.html