Quickly use unittest framework

1. Unittest framework principle.
To put it simply, it is to use the assert function in the unnittest framework to determine whether the program is normal when it is executed to a certain position, which is equivalent to the print data we usually use when testing the program.

2. Unittest needs to be used
2.1 Inherit the unittest.TestCase class
2.2 Each method to be tested must start with test (tell unittest this function to test the function)
2.3 Each test will call the two functions setUp and tearDown
2.4 Run the unittest framework to use unittest.main (), there is another method that is too troublesome to remember.
3. How to see the result of the execution

#1.导入 unittest 库
import unittest

#测试类必须继承unittest.TestCase

class test_(unittest.TestCase):
    #测试方法必须以 test_ 开头告诉unittest这个是测试方法
    
    def setUp(self):
        self.s = "hello world!"
        print("test is start s is %s" % self.s)
    
    def test_str_split(self):
        
        
        self.assertEqual(self.s.split(),["hello","world!"])
    
    def test_1(self):
        self.assertEqual(4 + 6,9) 
    
    def test_2(self):
        self.assertNotEqual(5 * 2,10) 
    
    def test_3(self):
        self.assertTrue(4 + 5 == 9,"The result is False")
    
    def test_4(self):
        self.assertTrue(4 + 5 == 10,"assertion fails")
    
    def test_5(self):
        self.assertIn(3,[1,2,3])  
    
    def test_6(self):
        self.assertNotIn(3, range(5))
    
    def test_7(self):
        self.assertAlmostEqual(22.0/7,3.14) 
    
    def test_8(self):
        self.assertAlmostEqual(22.0/7,3.14,4,"test is error") 
    
    def tearDown(self):
        print("test over")

if __name__ == '__main__':
    unittest.main()  #运行unnittest框架
#运行结果   


:\测试\unnittest框架>python unit


test_test.
py
test is start s is hello world!  #每次运行test的时候都是调用setUp 和tearDown函数
test over
Ftest is start s is hello world!
test over
Ftest is start s is hello world!
test over
.test is start s is hello world!
test over
Ftest is start s is hello world!
test over
.test is start s is hello world!
test over
Ftest is start s is hello world!
test over
Ftest is start s is hello world!
test over
Ftest is start s is hello world!
test over
.   
#只会打印报错的信息
======================================================================
FAIL: test_1 (__main__.test_)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "unittest_test.py", line 20, in test_1
    self.assertEqual(4 + 6,9)
AssertionError: 10 != 9

======================================================================
FAIL: test_2 (__main__.test_)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "unittest_test.py", line 23, in test_2
    self.assertNotEqual(5 * 2,10)
AssertionError: 10 == 10

======================================================================
FAIL: test_4 (__main__.test_)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "unittest_test.py", line 29, in test_4
    self.assertTrue(4 + 5 == 10,"assertion fails")
AssertionError: False is not true : assertion fails

======================================================================
FAIL: test_6 (__main__.test_)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "unittest_test.py", line 35, in test_6
    self.assertNotIn(3, range(5))
AssertionError: 3 unexpectedly found in range(0, 5)

======================================================================
FAIL: test_7 (__main__.test_)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "unittest_test.py", line 38, in test_7
    self.assertAlmostEqual(22.0/7,3.14)
AssertionError: 3.142857142857143 != 3.14 within 7 places (0.0028571428571426694 difference)

======================================================================
FAIL: test_8 (__main__.test_)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "unittest_test.py", line 41, in test_8
    self.assertAlmostEqual(22.0/7,3.14,4,"test is error")  #test_8 中调用的断言函数
AssertionError: 3.142857142857143 != 3.14 within 4 places (0.0028571428571426694 difference) : test is error(这个是出错打印的字符串)

----------------------------------------------------------------------
Ran 9 tests in 0.016s

FAILED (failures=6)             #一共出错的地方有8处
'''
总结
运行结论
1.总共有6个出错的分别是
test_8 7 6 4 2 1 6个测试方法
Published 5 original articles · Likes0 · Visits 57

Guess you like

Origin blog.csdn.net/qq_40715157/article/details/105642313