Most people who do testing know the tools and frameworks for testing interfaces , Common tools are jmeter and postman; Automation frameworks generally have
unittest、pytest and testng, among unittest、pytest 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
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 .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
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
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 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 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)