The difference between unittest and pytest in python unit testing framework

Preface

Today I want to talk to you about the Python unit testing framework. We all know that there are many Python unit testing frameworks. The one we usually use is unittest because it is relatively basic and can be used for secondary development. If your development level is very high High, integrated development of automated testing platforms is also possible. This article mainly talks about the difference between unittest and pytest. Compared with unittest, pytest has simple code, convenient and flexible use, and rich plug-ins. Without further ado, let’s get straight to the topic.

1. Use case writing rules

1. Unittest provides test cases, test suites, test fixtures, and test runner related classes to make testing more clear, convenient, and controllable. When writing use cases using unittest, you must comply with the following rules:

  (1) The test file must first import unittest
  (2) The test class must inherit unittest.TestCase
  (3) The test method must start with "test_"
  (4) The test class must have the unittest.main() method

2. Pytest is a third-party testing framework for python. It is an extension framework based on unittest and is simpler and more efficient than unittest. When writing use cases using pytest, you must comply with the following rules:

(1) The test file name must start with "test_" or end with "_test" (such as: test_ab.py)
(2) The test method must start with "test_".
(3) The test class name starts with "Test".

Summary: pytest can execute unittest style test cases without modifying any code of the unittest case, and has good compatibility. Pytest has a wealth of plug-ins, such as the flask plug-in, which can be used to rerun use cases when errors occur; and the xdist plug-in, which can be used for parallel execution of devices. 

2. Pre- and post-use cases

1.unittest provides setUp/tearDown, which can only be used for all use cases.

2.pytest provides module-level, function-level, class-level, and method-level setup/teardown, which is more flexible than unittest’s setUp/tearDown.

The module level (setup_module/teardown_module) starts at the beginning and end of the module. The global
function level (setup_function/teardown_function) only takes effect for function use cases (not in the class).
The class level (setup_class/teardown_class) only runs once before and after the class (in the class)
The method level (setup_method/teardown_method) starts at the beginning and end of the method (in the class) and the
(setup/teardown) inside the class runs before and after calling the method.

3.pytest can also add the @pytest.fixture() decorator before the function and install it in the fixture function in the test case. The usage scope of fixture can be function, module, class, session.
Firture has the following advantages over setup and teardown:

The naming method is flexible and is not limited to the naming of setup and teardown
. Data sharing can be achieved in the conftest.py configuration. Some configurations can be automatically found without import and can be called by multiple py files.
scope="module" can realize multiple .py cross-file sharing with prefix
scope="session" to realize multiple .py cross-files. Use one session to complete multiple use cases.
Use yield to wake up the execution of teardown.

3. Assertion

1.unittest provides assertEqual, assertIn, assertTrue, and assertFalse.

2.pytest uses assert expression directly.

4. Report

1.unittest uses the HTMLTestRunnerNew library.

2.pytest has pytest-HTML and allure plug-ins.

5. Rerun after failure

1. Unittest does not have this function.

2. Pytest supports rerun if the use case execution fails, pytest-rerunfailures plug-in.

6. Parameterization

1. Unittest needs to rely on the ddt library.

2. Pytest directly uses the @pytest.mark.parametrize decorator.

7. Use case classification execution

1. Unittest executes all use cases by default, and you can also execute some use cases by loading testsuit.

2. Pytest can mark classes and methods through @pytest.mark. Adding parameters ("-m") to pytest.main can only run the marked classes and methods.

8. If it doesn’t look good, you can look at the following table:

 Generally speaking, the unittest use case format is complex, has no compatibility, has few plug-ins, and is convenient for secondary development. pytest is more convenient and faster. The use case format is simple. It can execute unittest style test cases without modifying any code of the unittest use case. It has better compatibility. There are many pytest plug-ins, such as the flask plug-in, which can be used to rerun use cases when errors occur, and the xdist plug-in, which can be used for parallel execution of devices and is more efficient.

Finally: The following are supporting learning materials. For those who are doing [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covers the following interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics

  How to obtain the full set of information: Click on the small card below to get it yourself

Guess you like

Origin blog.csdn.net/weixin_57794111/article/details/132900758