当前位置:网站首页>Pytest test framework Basics

Pytest test framework Basics

2022-06-11 16:17:00 Abstinence Libai lisage

Today I'd like to share with you pytest Basics of test framework , The content includes :

-  understand pytest Features of the test framework

-  master pytest Basic use

-  master pytest Common scenes

-  master pytest Operating parameters and configuration methods

Catalog

One 、 The role and type of testing framework

1.1 The test framework

1.2 About Python Test framework for

Two 、pytest The test framework  

2.1  install pytest

2.2  start-up pytest

2.3  Write test cases 【 a key 】

2.3.1 Discovery rules

2.3.2  Practice method

2.4  Operation parameters and their configuration

2.4.1  Configuration item

2.4.2  Configuration change

3、 ... and 、 Conclusion


One 、 The role and type of testing framework

1.1 The test framework

Concept : The test framework , Discover test cases 、 organization 、 perform 、 Record 、 Report and other management operations , So as to help testers better design 、 Develop automated test scripts .

1.2 About Python Test framework for

- UnitTest
- pytest
- Robot Framwork == RF

frame advantage shortcoming
RF

Keyword Driven

Rich plug-ins

Good scalability

It is not convenient to customize the test report

Inconvenient to test

UnitTest

python built-in

xUnit usage

Easy to use

Naming style and Python atypism

Too much support for abstraction , object-oriented

Missing plug-in , There's no way to expand

Pytest

Object orientation is not required

Very flexible to use

Rich plug-ins

Incompatible with other frameworks

Requirements python Basics

Common ground : 

1. Tag and filter use cases
2. Execute use cases
3. Record the test process
4. You can manage the test environment
5. Generate test reports

Two 、pytest The test framework  

pytest It's more pythonic Test framework for , With strong style characteristics .

pythonic, A simple translation is python style ,pythonic code, That is to say, the code you write should have a strong python Normative and python style

2.1  install pytest

pip install pytest

Be careful :pytest The ecology of pytest Itself and the pytest Plug-ins together , In addition to itself, you also need some plug-ins .

  • pytest:pytest Frame body

  • pytest-html: Generate HTML Test report

  • pytest-xdist: Parallelize execution of test cases

  • pytest-rerunfailures: Fail to run again

  • pytest-ordering: Sort the use cases

  • allure-pytest: Generate allure Test report ( The more advanced )

One click installation of all :

pip install pytest pytest-html pytest-xdist pytest-rerunfailures pytest-ordering allure-pytest

2.2  start-up pytest

1. Execute enter... On the command line pytest ---- Where to install , Just type in... Somewhere ( Suggest )

2.python Execute in code pytest.main()

3.pycharm Provides testrunner( Not recommended )

   - Execute enter... On the command line pytest effect

 Text Test report

1. Report header :
  - Platform and version information
  - root directory 、 The configuration file 、 Special options
  - Plug in list
2. Collect information
  - The number of test cases
3. Execution status
  - The execution result of the use case
  - Test execution progress
4. Abstract
  - Error message 、 Captured output
  - Results statistics
  - Time consuming Statistics

2.3  Write test cases 【 a key 】

2.3.1 Discovery rules

pytest Test case discovery rules :
1. Start from the current directory , Traverse each directory
2. Search the directory for test_*.py and *_test.py, And import
3. In the imported file , Collect test cases :
  - test Initial function
  - Test Initial class , And its test Opening method
  - unittest.TestCase Subclasses of , And its test Opening method (unitest Test cases can be pytest compatible , Equivalent to using pytest Framework to execute unittest The use case )

  The following are examples to illustrate

be based on test Initial function

def test_login(self):
	pass

be based on Test Initial class

class TestAPI:
    def test_login(self):
        pass

be based on unittest.TestCase Subclasses of  

import unittest

class test(unittest.TestCase): #  If it is unittest.Testcase Subclasses of , Naming is not required 
    def test_login(self):
        pass

2.3.2  Practice method

1. establish test_ Opening file :test_api.py
2. In the document , establish test_ Initial function :test_login
3. Use the assertion keyword in a function to assert :assert 

Expand : 

class TestAPI:
    
    def __init__(self): #  As a test case class  No instantiation is allowed __init__ Method 
        pass

    def test_login(self):
        pass

  The effect of the above code is as follows :

This means that classes used for testing cannot have initialization methods __init__.

reflection : 

If there are too many files , What should I do if I want to classify ?

Too many files can be put in any folder , Does not affect loading and execution ,pytest There are no requirements for folders .

practice : If the folder has everything related to the test , Use test perhaps test_ Let's start with .

2.4  Operation parameters and their configuration

2.4.1  Configuration item

It can be here pytest Read and view on the official website

pytest Official website :pytest: helps you write better programs — pytest documentation/

Or you can enter a command to help view

pytest -h

Here are some common configuration items :

  • -v Show details

  • -s Turn off output capture

  • -n X: Use X process , Parallelized running test cases ( A computer can write as many cores as it needs )

  • -n auto: Automatically select the number of processes to execute

  • --html=Path: Generate HTML Test report , And keep it in Path route

  • --self-contained-html:HTML File self-contained

  • --reruns X: After the test case fails , retry X Time  

2.4.2  Configuration change

adopt pytest.ini Profile to configure

[pytest]
addopts = -v -s -n 1 --html=report.html

The operation effect is as follows :

Each effect in our configuration parameters will be displayed

3、 ... and 、 Conclusion

Today's sharing is mainly about pytest Explain the basics , I will share it later pytest Advanced and practical test framework , Interested friends continue to pay attention to , It's not easy to create , Look at the third company .

原网站

版权声明
本文为[Abstinence Libai lisage]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111600363793.html