程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python interface automated testing (III) Python framework unittest

編輯:Python

Most people who do testing know the tools and frameworks for testing interfaces , Common tools are jmeter and postman; Automation frameworks generally have

unittestpytest and testng, among unittestpytest Is based on python Language testing framework ,testng Is based on Java Test framework for , Article selection for this series unittest Framework as a testing framework .

1、 What is? unittest

  • This is how the official website explains :unittest Unit testing framework is subject to JUnit Inspired by the , It has a similar style to the mainstream unit testing frameworks in other languages . It supports Test Automation , Configure shared and shutdown code tests . Support to aggregate test samples into test set , And separate testing from reporting framework .
  • I understand it : Actually unittest A framework has its own set of specifications , We need to follow this specification when using , It can make our testing easier , Better code maintenance , More reusability , The result is better than the original

2、unittest Component part

  • Test scaffolding

Official website definition :test fixture Indicates the preparatory work required to carry out one or more tests , And all the cleaning operations involved . for instance , This may include creating temporary or proxy databases 、 Catalog , Or start a server process .

In fact, scaffolding includes setup and teardown Two parts , In specific tests, I usually use these two parts for initialization and garbage data cleaning after the test

def setUp(self):
# initialization
pass
def tearDown(self):
# Data cleaning
# Or statistics
pass
  • The test case

unittest Provides a base class : TestCase , Used to create a new test case , Our test case class needs to inherit TestCase Base class

 class demo(unittest.TestCase):
  • test suite

test suite Multiple use cases can be executed together

if __name__ == "__main__":
suit = unittest.TestSuite()
suit.addTest(createCode('test01_createCode'))
result = unittest.TextTestRunner().run(suit)
  • Test runner

test runner Is a component for executing and outputting test results . This runner may use a graphical interface 、 Text interface , Or return a specific value that represents the result of running the test .

result = unittest.TextTestRunner().run(suit)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved