Pytest summary

  • One of the great things about Pytest is that it can extend many plug-ins.
  • Install python toolkit, less than length have access to the warehouse, use the -i command specified warehouse address, I usually is to choose the address: https://mirrors.ustc.edu.cn/p…
  • Pytest plugins include the following features:
  1. Install pytest: pip3 install pytest -i https://mirrors.ustc.edu.cn/p…
  2. Since the project tests are expected to be executed repeatedly, the pytest-repeat plug-in is used –count=2, specifying the number of tests, and the default is the function-level test –repeat-scope, specifying the domain to be executed repeatedly.

    • Pytest has a concept of a domain, namely scope, which is divided into function, class, module, session
    • Where function refers to a function that starts with test, class refers to a class that starts with test, module refers to a file that starts with test, and session refers to all the use cases executed this time.
    • So if –repeat-scope specifies session, repeat by session.
    • Note: It has been tested — the Xdist command does not execute as expected repeat-scope.
    • Example:pytest test1.py -s --count=5 --repeat-scope=session
    • Specify the number of times a function is executed by adding the @PyTest.mark.repeat (count) decoration to the function.
    @pytest.mark.repeat(5) def test_02(start, open_baidu): print(" test case test_02")
  3. PyTest-xdist, which executes multiple non-dependent use cases concurrently
  • The — Xdist option is designed to save the execution time of test cases. Multiple cores are used to execute test cases, provided that there are no dependencies in the use cases. Dependancy can be found if ordering or ordering can’t be guaranteed
  • Install pip3 install pytest – xdist -i https://mirrors.ustc.edu.cn/p…
  • Usage:pytest test.py -n autoIndicates that the use case is automatically assigned a reasonable number of cores to run based on the current CPU information, also availablepytest test.py -n 3Specifies the number of cores to run and the number of concurrent processes
  1. PyTest is a relatively simple plug-in that ensures the ordering of project use cases
  • Installation: pip3 install pytest – ordering -i https://mirrors.ustc.edu.cn/p…
  • Usage:
@pytest.mark.run(order=7)
   def test_delete_port(self, host_ip, module_dict, cfg):
       if "test_create_port" not in module_dict:
           # skip
           pytest.skip("do not have test_create_port result")

       self.delete_port(host_ip, module_dict)

   @pytest.mark.run(order=8)
   def test_delete_subnet(self, host_ip, module_dict, cfg, db_session):
       if "test_create_vpc_subnet" not in module_dict:
           # skip
           pytest.skip("do not have test_create_port result")

       self.delete_subnet(host_ip, module_dict, db_session)

The two pieces of code execute 7 before 8, sorted by name if not specified.

  1. Custom parameter
  • Sometimes we need to pass in parameters for different scenarios, just refer to the following code:
Pytest's fixed fixture(firmware) can be used directly, and the fixed fixture(def) can be used to parse the field. parser.addoption("--cmdopt", action="store", default="dev.ini", help="my option: type1 or type2") parser.addoption("--concurrency", action="store", default='False', help="my option: Fixtures (scope="module") def cmdopt(request): --cmdopt @pytest. fixtures (scope="module") def cmdopt(request): return request.config.getoption("--cmdopt")
  • Refer to https://blog.csdn.net/waitan2… :
  1. Generate the report:
  • To use the simple pytest-html plug-in, simply install pip3 pytest-html and run the command pytest test.py — HTML =./report.html and specify the output file as the report.html report
  • Using Allure tool, the interface is nice, the function is full, the installation is complex

    • Allure plug-ins and tools need to be installed
    1. Allure Brew Install Allure
    2. Installing a plug-in pip3 install allure – pytest -i https://mirrors.ustc.edu.cn/p…
    3. Pytest-v test_vpc_floatingip.py-n auto –count= 5-q — allureredir./report
    4. Allure generate –clean report/ -o allure-results/ #
    5. Allure open. /allure-results/ opens the file
    6. Note: Allure uses two ways to render pages, Allure Open and Allure Serve. The former is used to render and view the results locally, and the latter is used to display the results after rendering locally, so to view HTML content with data, you need to use Allure Open. Run the command allure open allure-report to automatically open a browser (wait patiently) to display the rendered results, where allure-report is the directory where the results are generated by allure generate.
    7. Some people will use pip3 install pytest allure – adaptor -i https://mirrors.ustc.edu.cn/p… > attributeError: module ‘pytest’ has no attribute ‘allure’. Uninstall l allure-pytest
    8. Repeated multiple times, Allure will record multiple results, but the total number of use cases will remain the same, but the number of repeats of a particular use case will increase
  1. Important: My project test cases have dependencies, I don’t want to lose many test_case details by combining them together, and I want to execute the process multiple times in parallel. In this case, I cannot simply use Repeate ordering Xdist plug-in, so I can only use the following method: main function:
def run(i):
    file_name = '--html=./report/report_{}.html'.format(i[0])
    pytest.main(["-v", "-s", '--log-format="%(asctime)s %(levelname)s %(message)s"',
                 '--log-date-format="%Y-%m-%d %H:%M:%S"', file_name, '', '--alluredir', './report/result', i[1]])
    # pytest.main(['-v', '--alluredir', 'report/result'])


if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1:
        p1 = '--cmdopt={}'.format(sys.argv[1])
        num = int(sys.argv[2])
    else:
        p1 = '--cmdopt=dev.ini'
        num = 1

    import multiprocessing
    pool = multiprocessing.Pool(4)

    _list = [(x, p1) for x in range(num)]
    pool.map(run, _list)
    pool.close()
    pool.join()
  1. About log output: You can specify –log-format=”%(asctime)s %(levelName)s %(message)s” and –log-date-format=”% y-% m-%d %H:% m :% s”, respectively
  2. The execution of a pytest can be divided into several types: The most common way to do this is to run pytest test.py. The second way to do this is to run pytest directly. The third way to do this is to run python test.py, but the contents of test.py in this case should be the same as the contents of pytest.main in the run from Section 7. Either way, pytest will load from pytest.ini first. If there is an overwritten message, it will be overwritten; otherwise, it will load from pytest.ini

Enable logging using log_cli=true

[pytest]
; addopts = -s -v --html=report/report.html --alluredir ./report/result --count=1 -n auto
testpaths = ./
#python_files = test_*.py
#python_classes = Test*
log_cli=true
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_level = INFO
#log_level=NOTSET
log_file = ./logs/pytest-logs.txt
python_functions = test*

Reference: https://www.jianshu.com/p/ddb… https://www.cnblogs.com/yoyok… Skip use cases https://blog.csdn.net/qq_42610167/article/details/101204066?depth_1-utm_source=distribute.pc_relevant.none-task&utm_sour ce=distribute.pc_relevant.none-task