What framework is used for automated testing? Pytest framework vs Unittest framework, enterprise usage analysis...


foreword

Different installation methods

unittest is a library that comes with python, no need to install
pytest is a third-party unit test library that needs to be installed (pip install pytest)

Pytest automated testing framework: https://www.bilibili.com/video/BV18K411m7FH/

Test case design rules are different

The test class in unittest needs to inherit the unittest.TsetCase class,
and the test class must have the unittest.main() method, and
the test function must start with "test_"

pytest does not need inheritance, it can be a test class or a test function;
the file name of the test file must start with "test_" or end with "_test", the
name of the test class must start with "Test", and the name of the test function must start with "test",
the "init" method cannot be used in the test class

Test cases are executed differently

unittest, you need to use the test suite suite to collect test cases first, then use TextTestRunner, and finally execute (run);

pytest, as long as there is a pytest.py file, pytest.main() can automatically collect use cases to execute, which will be much more convenient

Parameterization uses different

unitest, the use of parameterization needs to rely on third-party libraries, and use ddt for parameterization;

pytest, using parameterization is to directly use parametrize to achieve parameterization

Test reports are used differently

unittest, the test report is generated by HTMLTestRunner, or by importing the third-party library unittestreport;

pytest, generate html format reports through pytest-html, or generate through allure

Fixtures (front and rear) are used in different ways

unittest, each test file must be set separately
. Execute before each use case through setUp, execute after each use case in tearDown, execute before
executing all use cases in the setUpClass() class, and execute after executing all use cases in the tearDownClass() class. And it must be used together with the @classmethod decorator

pytest, create a conftest.py file, all test files in the current directory can automatically call the fixture;
through @pytest.fixture() to declare as a fixture, before the yield is the front, after the yield is the post, and can be used Parameters (scope) to set the scope of the fixture:
function: default scope, each function or method will be called
class: each test class will be executed only once module: each .py file will be called only once package: each python package
will be called only once
Execute a
session: the entire session is executed only once, that is, when the project is run, the entire process is executed only once.
When calling the fixture, just pass the fixture name directly as a parameter. You can
set the automatic call, and set the parameter autouse=True through the fixture
Therefore, the fixture of Pytest will be more convenient and simpler than that of unittest

fail rerun

unittest does not support re-running on failure;

pytest supports reruns on failure, pytest --reruns=2 (2 means reruns 2 times)

Pick use case execution

unittest does not support filtering use case execution

pytest supports marking use case execution
Mark the use case you need to run @pytest.mark.smoke (smoke is a custom name, it can be other) to
run the use case, pytest -m "smoke" (command line mode) or pytest.main([“ -m=smoke”]) (used by the main method)

Assertion method

unittest assertion:
assertEqual(a, b) # determine whether a and b are equal
assertNotEqual(a, b) # determine whether a is not equal to b
assertTrue(a) # determine whether a is True
assertFalse(a) # determine whether a is False
assertIn( a, b) # a is included in b
assertNotIn(a, b) # a is not included in b

pytest assertion:
pytest only needs to use assert to assert, just add the conditions that need to be asserted after assert, for example: assert a = = b # determine whether a is equal to b, assert a != b # determine that a is not equal to b,
assert a in b # judge that b contains a

fail fast

unittest does not support fast failure;

pytest can quickly fail the use case
by naming pytest --x, and stop running once the use case fails.
It can also stop running by pytest --maxfail=10 how many times it fails

Migrating from unittest to pytest

The following situations cannot be mixed with pytest and unittest:
After inheriting unittest.TestCase, the fixture and parameterization of pytest cannot be used, otherwise an error will be reported

Migration strategy:
1. First use pytest to collect use cases, run use cases, generate test reports, and use pytest other features except fixtures and parameters, such as use case screening features, repeated running features, and fast failure 2. Parameterization, change to
@pytest .mark.parametrize()
3. Migration of fixtures:
1) Use an independent class to store (involves other changes, not recommended);
2) fixture (define a method to store the front and back, and add the decorator @pytest on it .fixture(scope="class"));
3) setup_class() setup_method() (this is the smallest change, just change the method name, it is recommended to use this)

Summary
Pytest's simple and intuitive syntax makes writing test cases easy. Compared to other testing frameworks, the learning curve is gentle.
Feature-rich: Pytest provides many built-in plugins and extensions that can support various types of tests, including functional tests, performance tests, concurrency tests, etc.
Flexibility: Pytest is highly flexible, and the testing process can be easily extended and customized, making it adaptable to various testing needs.
Easy to integrate: Pytest can be well integrated with other Python libraries and tools, such as Selenium, Appium, Docker, Jenkins, etc., making the entire testing process more complete and automated.

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

As long as you still have breath, don't stop struggling. Do not forget your original intention and forge ahead. Only by persevering can you finally realize your ideals and wishes.

Every effort will not be let down, and every persistence will usher in harvest. May you work hard with no distractions and finally realize your dreams!

As long as you are willing to work hard, there is no peak that cannot be climbed; as long as you persist in fighting, there is no difficulty that cannot be overcome. Believe in yourself, move forward steadily, and success will eventually belong to you!

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/130156222
Recommended