Automated smoke testing: Unittest vs Pytest framework

This article describes the following points:

1. How Unittest organizes smoke cases
2. Pytest organization smoke test
3. Pytest executes unittest smoking case

Environmental preparation:

  • Python 3.x
  • Pytest 5.x

Project directory:

smoke_testing_demo
test_case
__init__.py
test_case_with_unittest.py
test_case_with_pytest.py
run_unittest_smoke_testing.py

1, How Unittest organizes smoke cases

  • When importing unittest, the TestLoader class will be automatically imported
  • Under the TestLoader class, five methods for organizing use cases are encapsulated
  • This article mainly explains loadTestsFromNames

Introduction to the loadTestsFromNames method

# loader. Pythe file no longer exists in python3.7. It is recommended to use python3.64 to view the usage

class TestLoader(object):
    """
    This class is responsible for loading tests according to various standards and packaging them in TestSuite in
    """


def loadTestsFromNames(self, names, module=None):
    """
    Returns the suite of test cases for a given set of use case names
    """ 

loadTestsFromNames organization smoke case

# test_case_with_unittest.py

#!/usr/bin/env python3
# encoding:utf-8

import unittest
class TestUittestCase(unittest.TestCase):

      def test_case_with_unittest_1(self):
          '''Smoke test case'''
          print('I am Smoke Testing ')

      def test_case_with_unittest_2(self):
          pass


if __name__ == '__main__':
    unittest.main(verbosity=2)
# test_case_with_unittest2.py

#!/usr/bin/env python3
# encoding:utf-8

import unittest

class TestUittestCase2(unittest.TestCase):

      def test_case_with_unittest_3(self):
          '''Smoke test case'''
          print('I am Smoke Testing ')

      def test_case_with_unittest_4(self):
          pass


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

Smoke test case set

# run_unittest_smoke_testing.py

#!/usr/bin/env python3
# encoding:utf-8

import unittest

cases = [
'test_case.test_case_with_unittest2.TestUittestCase2.test_case_with_unittest_3',
'test_case.test_case_with_unittest.TestUittestCase.test_case_with_unittest_1'
]
test_suit = unittest.TestLoader().loadTestsFromNames(cases)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(test_suit)

Running results

test_case_with_unittest_3(test_case.test_case_with_unittest2.TestUittestCase2)
Smoke testing  ... ok
test_case_with_unittest_1 (test_case.test_case_with_unittest.TestUittestCase)
Smoke testing  ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s

Summary:

  • loadTestsFromNames allows you to organize specific use case sets from different modules
  • To use the loadTestsFromNames method, you need to pass in an array
  • The elements in the array must be strings
  • Array element input format: 'moudlename testCaseClassName. testCaseName’
  • The execution case is executed according to the order of array elements

2, Pytest organization smoke test

  • pytest provides a test case marking mechanism
  • A test case can be used by multiple @pytest Mark
  • Same @pytest Mark can mark multiple test cases
  • pytest.mark is often used to organize smoke test cases

pytest.mark organization smoking case

# run_unittest_smoke_testing.py

#!/usr/bin/env python3
# encoding:utf-8

import pytest

@pytest.mark.test_env def test_case_1():
    pass

@pytest.mark.test_env @pytest.mark.smoke def test_case_2():
    ''' Smoke case'''
    pass

cd entry /test_case directory, using the command line to run test_case_with_pytest.py

pytest test_case_with_pytest.py -v -m smoke

Running results

collected 2 items
test_case_with_pytest.py::test_case_2 PASSED

============================== 1 tests deselected ==============================
==================== 1 passed, 1 deselected in 0.01 seconds ====================
Run marked test_env Use cases for

pytest test_case_with_pytest.py -v -m test_env
 Running results

collected 2 items
test_case_with_pytest.py::test_case_1 PASSED
test_case_with_pytest.py::test_case_2 PASSED
=========================== 2 passed in 0.01 seconds ===========================

3, Pytest executes Unittest smoking case

Pytest test framework is compatible with Python's own Unittest. Modify test_case_with_unittest2.py

# test_case_with_unittest2.py

#!/usr/bin/env python3
# encoding:utf-8

import unittest
import pytest

class TestUittestCase2(unittest.TestCase):

@pytest.mark.smoke
def test_case_with_unittest_3(self):
    '''Smoke test case'''
    print('I am Smoke Testing ')

def test_case_with_unittest_4(self):
    pass


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

Run test from the command line_ case_ with_ unittest2.py

pytest test_case_with_unittest2.py -v -m smoke

Running results

collected 2 items / 1 deselected / 1 selected
test_case_with_unittest2.py::TestUittestCase2::test_case_with_unittest_3 PASSED [100%]

============== 1 passed, 1 deselected, 1 warnings in 0.01 seconds ==============

Summary:

  • 1. Uittest organizes smoking cases. You need to specify test cases in different test modules through loadTestsFromNames, assemble them into test suites, and run them to TextTestRunner.
  • 2. Pytest organizes smoking cases. You only need to add @pytest mark. Key, use the command line pytest -m key test_case.py.
  • When using Uittest to organize smoke tests, there are at least two concerns: 1. When writing smoke tests for new functions, you need to maintain the smoke test case set; 2. When merging code, if two people modify the smoking use case set at the same time, the conflict must be resolved to prevent the smoking use case from being omitted
  • When using Pytest to organize smoke tests, the focus is on the use case itself. When writing smoke tests for new functions, you only need to add a @Pytest Mark, for example @Pytest mark. smoke.

Here, I would like to recommend my own software testing learning Q group: 746506216, which is all about testing. If you want to learn or are learning testing, you are welcome to join. Everyone is a testing party and shares dry goods from time to time (only related to software testing), including a copy of the latest Python automated testing advanced materials and zero foundation teaching in 2022 compiled by myself, Welcome to join us for advanced and interested test partners!

Tags: Python unit testing Programmer software testing IT Software automation functional test

Posted by D_tunisia on Fri, 03 Jun 2022 00:09:39 +0530