Python+selenium [Chapter 7] Unittest learning

 

  • Python+selenium [Chapter 7] Unittest learning

 

unittest is a built-in unit testing framework in python. It has the functions of writing use cases, organizing use cases, executing use cases and so on. It can be combined with selenium for UI automatic testing, and can also be combined with appium, requests and other modules for other automatic testing.
Before using unittest, you need to be familiar with the five concepts of the framework:
  testcase,testsuite,testLoader,testrunner,testfixture.
  
  1. testcase: a complete test unit. The execution of the test unit can complete the verification of a problem, which is fully reflected in the pre test environment setup, the execution of test code (run), and the post test environment restore (tearDown);
  2. testsuite: a collection of multiple test cases, test suite or test plan;
  3. testLoader: loads TestCase into TestSuite, where loadTestsFrom__ () method is used to find testcases, create their instances, add them to TestSuite, and return TestSuite instances;
  4. testrunner: execute the test cases and save the test results to the TextTestResult instance, including how many test cases have been run, how many have succeeded, how many have failed, etc;
  5. testfixture: initialization preparation and environment restoration of a test case, mainly the setUp() and setDown() methods
 

Basic steps for unittest:

  • a) Introducing unittest module with import statement
  • b) Let all classes that execute tests inherit from the TestCase class. TestCase can be regarded as a collection of methods that test a specific class
  • c) The initialization before the test is performed in the setup () method and the cleanup after the test is performed in the teardown() method. They are all methods in TestCase
  • d) The best way to write a test is to start with test (you can run it directly) deftest_add(self) and deftest_sub(self), etc. you can write multiple test cases to test the tested object
  • e) In the process of writing test methods, use the methods provided by TestCaseclass to test function points, such as assertEqual
  • f) Call unittest The main () method runs all methods that start with test
 
 

Introduction to common assertions of unittest:

 

Assertion is to compare the expected results with the actual results.

assertEqual(a,b) a==b # is it equal

assertNotEqual(a,b)  a!=b # is it unequal

assertTrue(x) bool(x) is True # or not

assertFalse(x) bool(x) is False # is it False

assertIn(a,b) a # in b

Assert greater (a, b) a > b # greater than

assertGreaterEqual(a,b) a > = B # greater than or equal to

assertLess(a,b) a < b# less than

assertLessEqual(a,b) a < = B # less than or equal to

 

You can add custom test failure information to the last parameter of the assertion, such as:

assertEqual(10,20, "10! = 20 test failed") displays this message when the assertion fails.

 
  • Code example
 
  • unittest_demo_14.py
# -*- coding: utf-8 -*-
# @Time : 2021/12/30 14:24
# @Author : Limusen
# @File : unittest_demo_14


import unittest

class TestCase01(unittest.TestCase):

    def setUp(self) -> None:
        print("Pre execution")

    def tearDown(self) -> None:
        print("Post execution information")

    def test_case01(self):
        '''addition'''
        self.assertEqual(1 + 2, 3)

    def test_case02(self):
        self.assertEqual(1 + 2, 2)


if __name__ == "__main__":
    TestCase01().test_case01()

 

  • Code example

unittest_demo_15.py

# -*- coding: utf-8 -*-
# @Time : 2021/12/30 14:35
# @Author : Limusen
# @File : unittest_demo_15

#  Assertion exercise

import time
import unittest


class TestCase02(unittest.TestCase):

    def setUp(self) -> None:
        # print("Pre execution")
        pass

    def tearDown(self) -> None:
        # print("Post execution information")
        pass

    def test_case01(self):
        a = 1 == 1
        self.assertTrue(a, 'assertion failure ,no True')

    # def test_case02(self):
    #     a =1 ==3
    #     self.assertTrue(a,'assertion failure ,no True')

    def test_case03(self):
        str = "Do you have a dream"
        self.assertTrue(str.__contains__("Dream"))

    def test_case04(self):
        name = ['Zhang San', 'Li Si', 'Wang Wu']
        self.assertIn('Li Si', name)
        print("Range test")

    def test_case05(self):
        self.assertIsInstance('lishou', str)
        print("Type determination")


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

 

  • Change code execution order

demo_unittest_15.py

# -*- coding: utf-8 -*-
# @Time : 2021/12/30 14:35
# @Author : Limusen
# @File : unittest_demo_15


import unittest


class TestCase03(unittest.TestCase):

    def test_case01_aaa(self):
        print("Execute use case:test_aaa")

    def test_case02_ddd(self):
        print("Execute use case:test_ddd")

    def test_case03_ccc(self):
        print("Execute use case:test_ccc")

    def test_case04_bbb(self):
        print("Execute use case:test_bbb")


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

    # # Method of changing execution order
    # suite = unittest.TestSuite()
    # suite.addTest(TestCase03('test_bbb'))
    # suite.addTest(TestCase03('test_ccc'))
    # unittest.main(defaultTest='suite')
    pass

    # Change name test_case04_bbb
    unittest.main()

 

  • Skip use case
# -*- coding: utf-8 -*-
# @Time : 2021/12/30 14:35
# @Author : Limusen
# @File : unittest_demo_15


import unittest


class TestCase04(unittest.TestCase):

    def test_case01(self):
        print("Computing test_case01")
        self.assertEqual(3 + 4, 7, "Summation failed")

    @unittest.skip("Unconditional skip")
    def test_case02(self):
        print("Computing test_case02")
        self.assertEqual(4 + 4, 8, "Summation failed")

    @unittest.skipIf(True, 'Skip if condition is true')
    def test_case03(self):
        print("Computing test_case03")
        self.assertEqual(4 + 4, 8, "Summation failed")

    @unittest.skipIf(False, 'If the condition is false, do not skip')
    def test_case04(self):
        print("Computing test_case04")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.skipUnless(False, 'Skip if condition is false')
    def test_case05(self):
        print("Computing test_case05")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.expectedFailure  # Failure does not record the number of failures
    def test_case06(self):
        print("Computing test_case06")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.expectedFailure
    def test_case07(self):
        print("Computing test_case07")
        self.assertEqual(4 + 4, 9, "Summation failed")


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

 

  • Collect use cases for running
# -*- coding: utf-8 -*-
# @Time : 2021/12/30 14:35
# @Author : Limusen
# @File : unittest_demo_15



import unittest


class TestCase05(unittest.TestCase):

    def test_case01(self):
        print("Computing test_case01")
        self.assertEqual(3 + 4, 7, "Summation failed")

    @unittest.skip("Unconditional skip")
    def test_case02(self):
        print("Computing test_case02")
        self.assertEqual(4 + 4, 8, "Summation failed")

    @unittest.skipIf(True, 'Skip if condition is true')
    def test_case03(self):
        print("Computing test_case03")
        self.assertEqual(4 + 4, 8, "Summation failed")

    @unittest.skipIf(False, 'If the condition is false, do not skip')
    def test_case04(self):
        print("Computing test_case04")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.skipUnless(False, 'Skip if condition is false')
    def test_case05(self):
        print("Computing test_case05")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.expectedFailure  # Failure does not record the number of failures
    def test_case06(self):
        print("Computing test_case06")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.expectedFailure
    def test_case07(self):
        print("Computing test_case07")
        self.assertEqual(4 + 4, 9, "Summation failed")


if __name__ == "__main__":
    testsuite = unittest.TestSuite(unittest.makeSuite(TestCase05))
    unittest.main(defaultTest='testsuite')

 

  • implement
# -*- coding: utf-8 -*-
# @Time : 2021/12/30 14:35
# @Author : Limusen
# @File : unittest_demo_19

import unittest


class TestCase06(unittest.TestCase):

    def test_case01(self):
        print("Computing test_case01")
        self.assertEqual(3 + 4, 7, "Summation failed")

    @unittest.skip("Unconditional skip")
    def test_case02(self):
        '''Unconditional skip'''
        print("Computing test_case02")
        self.assertEqual(4 + 4, 8, "Summation failed")

    @unittest.skipIf(True, 'Skip if condition is true')
    def test_case03(self):
        '''Skip if condition is true'''
        print("Computing test_case03")
        self.assertEqual(4 + 4, 8, "Summation failed")

    # @unittest.skipIf(False, 'If the condition is false, do not skip')
    @unittest.skip("Unconditional skip")
    def test_case04(self):
        '''Unconditional skip'''
        print("Computing test_case04")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.skipUnless(False, 'Skip if condition is false')
    def test_case05(self):
        '''Skip if condition is false'''
        print("Computing test_case05")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.expectedFailure  # Failure does not record the number of failures
    def test_case06(self):
        print("Computing test_case06")
        self.assertEqual(4 + 4, 9, "Summation failed")

    @unittest.expectedFailure
    def test_case07(self):
        print("Computing test_case07")
        self.assertEqual(4 + 4, 9, "Summation failed")


if __name__ == "__main__":
    # # Method 1: load all the use cases under the test class
    # suite01 = unittest.TestLoader().loadTestsFromTestCase(TestCase06)
    # unittest.main(defaultTest='suite01')
    #
    # # Method 2: load all the use cases under the test module
    # # Import the module itself first
    # suite02 = unittest.TestLoader().loadTestsFromModule(test_case6)
    # unittest.main(defaultTest="suite02")
    #
    # # Method 3: use string to load use cases
    # suite03 = unittest.TestLoader().loadTestsFromName('test_case6.TestCase06.test_case01')
    # # Whole module
    # suite04 = unittest.TestLoader().loadTestsFromName('test_case6.TestCase06')
    # suite05 = unittest.TestLoader().loadTestsFromName('test_case6')
    #
    # # Single load all test cases
    # all_suite = unittest.TestSuite()
    # all_suite.addTests(suite01)
    # all_suite.addTests(suite02)
    # all_suite.addTests(suite03)
    # all_suite.addTests(suite04)
    # all_suite.addTests(suite05)
    pass

 

Added by hmemnon on Thu, 30 Dec 2021 21:42:19 +0200