当前位置:网站首页>Unittest test framework

Unittest test framework

2022-06-12 08:59:00 Ma TA Fei Yan &lin_ li

Unittest The test framework

1、UintTest yes python Built in unit test framework , Have the ability to write use cases 、 Organize use cases 、 Execute use cases 、 Conditions for automated frameworks such as output reports

2、UintTest The unit testing framework is not only applicable to unit testing , It can also be applied to WEB automated testing 、 Development and implementation of interface automation test . It has a wide range of uses

Four components

testcase testfixture textsuite testrunner
 Insert picture description here

test suite :TestSuite

The common methods are as follows :
⑴unittest.TestSuite()
①addTest(): Add a single test case method
②addTests([…]): Add multiple test case methods , There is a list of method names
⑵unittest.TestLoader()
①loadTestsFromTestCase( Test class name ): Add a test class
②loadTestsFromModule( Module name ): Add a module
③discover( The directory of the test case ): Specify the directory to load , It will automatically find all test cases in this directory that conform to the naming rules

import unittest
class Test(unittest.TestCase):
    def test_1(self):
        print(1)
    def test_3(self):
        print(3)
    def test_2(self):
        print(2)
    def test_4(self):
        print(4)
    def test_5(self):
        print(5)
    def add(self):
        print(" This sample will not be executed ")

if __name__ =='__main__':
    # Execute all use cases 
    # unittest.main()
    #  Create a suite 
    suit = unittest.TestSuite()
    #  Add use cases 
    # suit.addTest(Test("test_3"))
    suit.addTests([Test("test_3"),Test("test_2")])
    #  Execute use cases 
    run = unittest.TextTestRunner()
    run.run(suit)

Test firmware :TestFixture

1、 It is used to build and destroy the test case environment . That is, the environment construction before use case execution , Restore the environment after use case execution . By covering TestCase Of setUP() and tearDown() Method to implement

2、setUP() Method :
⑴ initialization . Execute... Before executing the test , Used to prepare the environment required for this test
⑵ For example, you need to log in before testing token And so on are the environments that test cases need
⑶ For example, you need to access the database in the test case , So it can be setUp() Database connection to initialize
⑷setUP() Method can also be used to define some public variables ( data )、 Code etc. . In this way, these public variables can be called in the entire test class 、 data

3、tearDown() Method :
⑴ Execute... After executing the test , Used to restore the environment before the next test ( Follow setUP In the same way )
⑵tearDown() The process of the method is very important , Leave a clean environment for the next test , So as not to affect the next test
⑶ For example tearDown() Clear the data generated in the database , Then close the connection

4、UintTest There are two ways to use the test firmware in the framework
⑴ One is based on the test method ( Class method ) For the dimension of setUp() and tearDown(): Before and after each test method is executed
⑵ One is to test classes (TestCase) For the dimension of setUpClass() and tearDownClass(): Execute once before and after each test class ( The test method will not be executed before and after )

The first way to use it :

import unittest
class Test(unittest.TestCase):
    def setUp(self) -> None:
        print(" Open the browser ")
    def test_1(self):
        print(1)
    def test_3(self):
        print(3)
    def test_2(self):
        print(2)
    def tearDown(self) -> None:
        print(" Close the browser ")

if __name__ =='__main__':
    #  Execute all use cases 
    unittest.main()

 Insert picture description here
The second way to use it :

import unittest
class Test(unittest.TestCase):

    @classmethod  #  Indicate that this is a class method executed in the dimension of class 
    def setUpClass(cls):
        print("=== Start executing tests ===")

    def test_1(self):
        print(1)
    def test_3(self):
        print(3)
    def test_2(self):
        print(2)

    @classmethod  #  Indicate that this is a class method executed in the dimension of class 
    def tearDownClass(cls):
        print("=== End test execution ===")
if __name__ =='__main__':
    #  Execute all use cases 
    unittest.main()

 Insert picture description here

Assertion

import unittest
from selenium import webdriver


class Test(unittest.TestCase):

    def setUp(self) -> None:
        self.driver = webdriver.Firefox()
        self.driver.get("https://www/baidu.com")
        self.title = self.driver.title
        print(self.title)
        return self.title

    def test1(self):
        self.assertEqual(self.setUp()," use Baidu Search , You will know ")


if __name__ == '__main__':
    unittest.main()

Skip the test

unittest Several decorators are provided to skip the test :

import unittest

class MyTestCase(unittest.TestCase):
    #  Skip the test , Parameter is the reason why the test was skipped 
    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    #  When the condition is true , Skip the test 
    @unittest.skipIf(1>3,"if true")
    def test_format(self):
        print("test_format")
        pass

    #  Unless the condition is true , Otherwise, skip the test 
    @unittest.skipUnless(1>3, "if not true")
    def test_windows_support(self):
        print("test_windows_support")
        pass

if __name__ == "__main__":
    unittest.main()

Subtest

In a test , Pass in different parameters to test the same method ,subTest Subtests can meet this requirement , And the failure of a single sub test does not affect the execution of subsequent sub tests .

import unittest

class NumbersTest(unittest.TestCase):

    def test_even(self):
        """ Test that numbers between 0 and 5 are all even. """
        for i in range(0, 6):
            with self.subTest(i==i):
                self.assertEqual(i % 2, 0)

if __name__ == "__main__":
    unittest.main()

 Insert picture description here

原网站

版权声明
本文为[Ma TA Fei Yan &lin_ li]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120854034171.html