快速使用unittest框架

1.unittest框架原理。
简单说一个,就是利用unnittest框架中的断言函数,判断程序执行到某一个位置的时候的是否正常,就相当于我们平时测试程序时使用到的打印数据。

2.unittest 使用需要
2.1 继承unittest.TestCase 类
2.2 每一个要测试的方法必须以test 开头(告诉unittest 这个函数测试函数)
2.3 每一个测试的时候都会调用setUp 和 tearDown这2个函数
2.4 运行unittest 框架使用 unittest.main(),还有另一种方法较麻烦懒得记。
3. 如何看执行的结果看后面的运行结果

#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个测试方法
发布了5 篇原创文章 · 获赞 0 · 访问量 57

猜你喜欢

转载自blog.csdn.net/qq_40715157/article/details/105642313
今日推荐