当前位置:网站首页>Unittest use
Unittest use
2022-06-21 07:07:00 【sl01224318】
Catalog
Introduce
unittest yes python Unit test framework , Be similar to Junit frame , Among them is 5 A more important concept , Let's share with you .
unittest Medium 5 Usage are test fixture,test case,test suite,test runner,test loader, Mastering these methods can better help us use unittest.
Conceptual analysis
TestCase
One TestCase The example of is a test case . A test case includes the setup of the pre test environment (setup), Execute test code , And the restoration of the environment after testing , unit testing (unittest) And that's the essence of it , A test case is a complete test unit , By running this test unit , You can verify a problem . for example :
class MyTestCase03(unittest.TestCase): # quote unittest.Testcase
def test01(self):
print('test01')
def test02(self):
print('test02')TestSuite
Multiple test cases come together , Namely TestSuite, It can also be understood as a collection of multiple test cases , and TestSuite You can also nest TestSuite.
TestRunner
TestRunner Is used to execute test cases , Among them run(test) Will execute TestSuite/TestCase Medium run(result) Method , for example :
class MyTestCase04(unittest.TestCase):
def test03(self):
print('test03')
def test04(self):
print('test04')
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite) # Use run() Method to execute the test case TestLoader
TestLoader It's used to load TestCase To TestSuite Medium , Some of them loadTestFrom Method , It's looking for it from all over the place TestCase, Create instances of them , then add To TestSuite in , Return to one more TserSuite example .
class MyTestCase04(unittest.TestCase):
def test03(self):
print('test03')
def test04(self):
print('test04')
if __name__ == '__main__':
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite.addTest(loader.loadTestsFromTestCase(MyTestCase04)) # Load test cases
Test fixture
Test Fixture Used before the test method , Or after the test method , The main function is to provide some devices for testing , These devices can be data , It can be an environment configuration or a pre run state , By covering TestCase() Of setUp() and tearDown() Method to implement , For example, we define a before the test case setup、teardown, In this way, each use case will be executed first setup、tewrdown Method .
class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
print('setup...')
def tearDown(self) -> None:
print('teardown...')
def test01(self):
print('test01')
def test02(self):
print('test02')
if __name__ == '__main__':
unittest.main()
边栏推荐
- 微信小程序_5,页面配置
- X86 CPU access DRAM and PCI
- Quantitative analysis of single cell transcriptome using cell Ranger
- Candy tunnel JS special effect code
- Two ideas of I2C driver implementation (I2C dev.c and I2C core.c)
- 打造硬核敲门砖——简历
- Wechat applet_ 5. Global configuration
- Argo CD usage
- Dynamic planning exercises (II)
- Bol Bohr's original dual currency driving model leads the new hotspot of dfi+nft+web3.0
猜你喜欢
随机推荐
0-1背包问题 (暴力递归 / 动态规划)
微信小程序_3,WXML模板语法
产品经理精通Axure工具篇
Consistency between database and cache data
Yield guild games and Walken reach cooperation
打造硬核敲门砖——简历
Hamming code verification [simple and detailed]
Crack the simple login system with NOP method
C language program design - Sanzi chess (semester homework)
C # basic knowledge series 8 (const and readonly keywords)
布隆過濾器
Summary of each layer of the five layer reference model
BOL波尔独创双币驱动模型 引领DeFi+NFT+Web3.0新热点
AdEx 治理投票:质押奖励减半
[GNN] Application of GNN neural network toolbox and MATLAB simulation
Microphone loading animation
Configuring the eigen3 development environment for vs2017 on win10
Understand this point
Pyg tutorial (6): customizing the messaging network
Google Earth engine (GEE) - US native lithology data set







