An article to get through, the pytest automated testing framework is detailed, from 0 to 1 proficient in actual combat (1)


foreword

pytest unit testing framework

1. What is a unit testing framework?
Unit testing refers to the correctness checking and testing of the smallest unit (function, method) of software in software development.

2. What are the unit testing frameworks?
Java: junit and testing
python: unittest and pytest

3. What does the unit testing framework mainly do?
Test discovery: Find our test cases from multiple files
Test execution: Execute according to a certain order and rules, and generate results
Test judgment: Judge the difference between expected results and actual results through assertions
Test report: Statistical test progress, consumption Time, pass rate, generate test report

What is the relationship between unit testing framework and automated testing framework

1. What is an automated testing framework?
Pack some functions or tools used in the automated testing process into various modules, including how to write automated scripts and the basic modules of layered functions, the management modules for continuous integration and timing tasks, sending test reports, and testing Statistical modules for statistical analysis of results, etc., and these modules form a set of reusable skeletons

2. Function
Improve test efficiency and reduce maintenance costs;
reduce manual intervention, improve test accuracy, and increase code reusability;
the core idea is: let people who do not know code can also use this framework to achieve automated testing;

3. The relationship between the pytest unit testing framework and the automated testing framework.
Unit testing framework: just one of the components of the automated testing framework.
Pom design pattern: just one of the components of the automated testing framework.
Data-driven, keyword-driven, and global configuration files Encapsulation, log monitoring, selenium, requests secondary encapsulation, assertion, report mail, etc.

Introduction to pytest

pytest is a very mature python unit framework, which is more flexible and easy to use than unittest;
pytest can be combined with selenium, requests, and appium to realize web automation, interface automation, and app automation;
pytest can realize test case skipping and reruns failure cases Retry;
pytest can generate very beautiful test reports with allure;
pytest can continuously integrate with Jenkins;
pytest has many very powerful plug-ins, and these plug-ins can implement many operations;

# 如
pytest
pytest-html             # (生成html格式的自动化测试报告)
pytest-xdist         # (测试用例分布式执行,多CPU分发)
pytest-ordering          # (用于改变测试用例的执行顺序)
pytest-rerunfailures  # (用例失败后重跑)
allure-pytest         # (用于生成美观的测试报告)

Install pytest

Install pytest:

pip install pytest 

Naming rules for pytest

The module name must start with test_ or end with test_;
the test class name must start with Test and cannot have an init method;
the test method must start with test;

D1

The running mode and execution order of pytest test cases

1. Operation mode
Main function mode
Run all: pytest.main()
Specified module: pytest.main([“test_01.py”])
Specified directory: pytest.main([“./test_py”])

Use nodeid to specify the use case to run: nodeid consists of module name, separator, class name, method name, and function name

pytest.main(["test_01.py::Test01Class"])  # 指定到类名
pytest.main(["test_01.py::Test01Class::test003"])  # 指定到方法名

2. Command line mode
Run all: pytest
Specified module: pytest test_01.py
Specified directory: pytest ./test_py

Run the use case specified by nodeid: pytest test_01.py::Test01Class::test0033. Run pytest.ini by reading the pytest.ini configuration file. This file is the core configuration file of the pytest unit test framework

Location: generally placed in the root
directory of the project Encoding: must be ANSI, you can use notepad++ to modify the encoding format
Function: Change the default behavior of pytest
Running rules: whether it is running in the main function mode or command line mode, it will read this configuration file

D2

3. Execution order
unittest: absolute execution order according to the size of Ascll
pytest: default from top to bottom
Change the default execution order: use mark mark

@pytest.mark.run(order=3)

Pytest generates its own html test report

Install pytest-html:

pip install pytest-html

1. Directly execute pytest.main() [Automatically search for files starting with test or py files ending with test in the current directory]

pytest.main([‘--html=./report.html’]) 

D3

2. pytest.main("module.py")【Run under the specified module, run all classes and test cases beginning with test】

pytest.main(["--html=./report.html","模块.py"])

D4

3. Run the specified module, specify the class, specify the use case, separate with a colon, and generate a test report

pytest.main([--html=./report.html’,‘模块.py::::test_a_001'])

D5

4. Pytest call statement

pytst.main(['-x','--html=./report.html','t12est000.py'])

-x: exit the test if a test case fails
-s: display print content
-v: display more detailed information
-vs: use these two parameters together
-n: support multi-threaded or distributed running test cases
-reruns NUM: Failed case re-run
-x: As long as there is an error in one test case, the test will stop
–maxfail=2: Stop if two test cases fail
-k: Specify the test case according to the partial string of the test case

5. Use @pytest.mark.skip() to skip the use case (function)

class Test01Class():
    @pytest.mark.skip  # 跳过test001
    def test001(self):
        assert 5 == 4  # 断言

D6

6. The character meaning of the pytest running result
.: dot, indicating that the use case passed
F: indicating failure Failure
E: indicating that there was an exception in the use case Error
S: indicating skipped

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)

On the stage of life, struggle is the most beautiful melody. Don't be afraid of difficulties, let your dreams fly, and water the growing flowers with sweat. Adhere to the belief and forge ahead. Only by constantly striving can we win our own glory and success.

There may not be rewards for paying, but there must be no gain if you don't pay. Facing difficulties bravely, persevering in efforts, fearless of failure. The blood of struggle will light up the road ahead, as long as you keep moving forward, the dawn of success will surely shine on your life.

Every day is a chance to be born again, go ahead and chase your dreams! No matter how many difficulties and obstacles lie ahead, as long as you have faith and work hard, you will shine brightly and let the world change for you.

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/132260310