unittestフレームワークをすばやく使用する

1.ユニットテストフレームワークの原則。
簡単に言えば、unnittestフレームワークのアサート関数を使用して、プログラムが特定の位置で実行されたときに正常かどうかを判断することです。これは、プログラムのテスト時に通常使用する印刷データと同等です。

2. unittestを使用する必要があります
2.1 unittest.TestCase クラスを継承します
2.2テストする各メソッドはtestで開始する必要があります(unittestにこの関数を通知して関数をテストします)
2.3各テストは2つの関数setUpおよびtearDownを呼び出し
ます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件公開しました Likes0 訪問数57

おすすめ

転載: blog.csdn.net/qq_40715157/article/details/105642313