Unit combat exercise testing Python

Examples of binding, contact the testing unit.

File Structure:

|----Python_unittest

| -------- math_operation.py # defines a class [class MathOperation:], which defines the class initialization function, and the arithmetic method of Example 4

| -------- two_num_delete.py # defines a class [class TestDelete (unittest.TestCase):], which defines the class subtraction test use cases

| -------- two_num_divide.py # defines a class [class TestDivide (unittest.TestCase):], which defines the class division by test cases

|--------unittest_suite.py

 


 1.two_num_delete.py

unittes Import 

from Python_unittest.math_operation Import MathOperation class the TestDelete (of unittest.TestCase): @classmethod DEF setUpClass (CLS): "" " before executing all use cases, calls setUpClass class properties : return: " "" log_file_name = "math_operation_result_log.txt " cls.one_file = open (log_file_name, MODE =" A ", encoding =" UTF-. 8 ") Print (F" [{log_file_name}] open file ") cls.one_file.write (" \ {n-: ^ = 40s } \ n ".format (" begin] [subtraction test case ")) @classmethod DEF tearDownClass (CLS): " "" after performing in all use cases,It calls once tearDownClass class attribute : return: "" " cls.one_file.write ( "{: = ^ 40s } \ n" .format ( " [] subtraction test case execution finished")) cls.one_file.close () DEF test_delete0 (Self): REAL_RESULT MathOperation = (. 9, . 6) .Delete () expect_value. 3 = the try: self.assertEqual (expect_value, REAL_RESULT, MSG = "subtract two numbers exception") Print ( "a> B> 0, the result of normal ab &") self.one_file.write ( "a> b> 0, the result is ab test: Pass \ n-") the except AssertionError with aS ERR: Print (F "specifically abnormalities: ERR {}") self.one_file.write (F "a> b> 0 when, as a result of the test ab: Fail \ n particular abnormality was ERR} { ")
the raise ERR  DEF test_delete1 (Self): real_result = MathOperation(-9, -6).delete() expect_value = -3 try: self.assertEqual (expect_value, REAL_RESULT, MSG = "subtract two numbers exception") Print ( "A <B <0, the result of normal ab &") when self.one_file.write ( "a <b <0 , ab test results: Pass \ n-") the except AssertionError with aS ERR: Print (F" specifically abnormalities: ERR {} ") self.one_file.write (F" a <B <0, the test results of ab: Fail \ n particular abnormality was ERR} { ")
The raise ERR

  


 2.two_num_divide.py

the unittest Import 

from Python_unittest Import MathOperation 


class TestDivide (of unittest.TestCase): 

    @classmethod 
    DEF setUpClass (CLS): 
        "" " 
        file are executed before executing all use cases 
        : return: 
        " "" 
        log_file_name = "math_operation_result_log.txt" 
        cls.one_file open = (log_file_name, MODE = "A", encoding = "UTF-. 8") 
        Print (F "[{log_file_name}] open file") 
        cls.one_file.write ( "\ {n-: 40s} = ^ \ n-" .format ( "begin [division] test case")) 

    @classmethod 
    DEF tearDownClass (CLS): 
        "" " 
        after the implementation of all use cases, called once tearDownClass class attribute 
        : return:
        """
        cls.one_file.write ( "{: = ^ 40s } \ n" .format ( " [] division of test case execution ended")) 
        cls.one_file.close () 

    DEF test_divide0 (Self): 
        REAL_RESULT MathOperation = (. 9, . 3) .divide () 
        expect_result. 3 = 
        the try: 
            self.assertEqual (expect_result, REAL_RESULT, MSG = "dividing the two abnormal") 
            Print ( "a> B> 0, the result of a / b normal") 
            self.one_file. write ( "a> b> 0 , a / b of the test results: Pass \ n-") 
        the except AssertionError with aS ERR: 
            Print (F "specifically abnormalities: ERR {}") 
            self.one_file.write (F "a> b> 0, the result of a / b is tested: Fail \ n particular abnormality was ERR} { ") 
the raise ERR DEF test_divide1 (Self): real_result = MathOperation(9, -3).divide() expect_result = 9 try: self.assertEqual (expect_result, real_result, msg = " dividing the two abnormal") Print ( "A> 0> B, the result of a / b Normal") self.one_file.write ( "A> B> 0, A / b test results: Pass \ n-") the except AssertionError with aS ERR: Print (F" specifically abnormalities: ERR {} ") self.one_file.write (F" a> b> 0 when, a / b of the test results It is: Fail \ n particular abnormality was ERR} {\ n-")
The raise ERR

  


3.unittest_suite.py

the unittest Import 

from Python_unittest Import two_num_divide AS num_divide 
from Python_unittest Import two_num_delete num_delete AS 

# define a] [kit 
one_suite unittest.TestSuite = () 

# define loader] [ 
# illustrated by loaded with the loader, and added to the kit 
one_loader = unittest.TestLoader () 

one_suite.addTest (one_loader.loadTestsFromModule (num_divide)) 
one_suite.addTest (one_loader.loadTestsFromModule (num_delete))
# Define runner] [one_runner = unittest.TextTestRunner () one_runner.run (one_suite) 

[Knowledge]

one_suite. addTest (one_loader.loadTestsFromModule (num_divide)) is added over Example plus test modules one by one to the kit

one_suite. addTests (one_loader.loadTestsFromModule (num_divide), one_loader.loadTestsFromModule (num_delete)) may be added to the plurality of test modules simultaneously

Related to the execution order and the order of addition.

 


4. Run Results:

[Knowledge]

Be sure to pay attention to catch the exception thrown when the total number of use cases are: the number of all methods beginning of test_, based on the number of failures is thrown [assertion] abnormal AssertionError to statistics.

The representative of the successful operation, F for fail.

Example performed with sequence:

loadTestsFromModele () method to add a module which will be executed first.

Inside the module loaded test is performed according to the order of ASCII code method.

defaultTestLoader.discover () The default load test sequence of ASCII code.

 


 

5.unittest_discover.py

import unittest

one_suite = unittest.defaultTestLoader.discover(".")
one_runner = unittest.TextTestRunner()

one_runner.run(one_suite)

[Knowledge]

def discover(self, start_dir, pattern='test*.py', top_level_dir=None):

The default load path to "test" the beginning of the module, the module test cases should all start test.

. Py represents the path of the current file is located.

You can also specify an absolute path: one_suite = unittest.defaultTestLoader.discover (r "C: \ Users \ zfy \ PycharmProjects \ Python_unittest")

 


 

6. Generated log file

math_operation_result_log.txt

 

 

Guess you like

Origin www.cnblogs.com/jszfy/p/11192301.html
Recommended