The whole network is super complete, and the pytest automated testing framework pytest.ini configuration file is detailed (actual combat)


foreword

The pytest configuration file can change the operation mode of pytest. It is a fixed file pytest.ini file, which reads the configuration information and runs according to the specified method.


Some files in non-test files
pytest are non-test files pytest.ini: the main configuration file of pytest, which can change the default behavior of pytest
conftest.py: some fixture configurations for test cases
init.py : identify this folder as a python package


Check the configuration option cmd execution of pytest.ini

pytest --help

find this part

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:

  markers (linelist):   markers for test functions
  empty_parameter_set_mark (string):
                        default marker for empty parametersets
  norecursedirs (args): directory patterns to avoid for recursion
  testpaths (args):     directories to search for tests when no files or directories are given in the command line.
  usefixtures (args):   list of default fixtures to be used with this project
  python_files (args):  glob-style file patterns for Python test module discovery
  python_classes (args):
                        prefixes or glob names for Python test class discovery
  python_functions (args):
                        prefixes or glob names for Python test function and method discovery
  disable_test_id_escaping_and_forfeit_all_rights_to_community_support (bool):
                        disable string escape non-ascii characters, might cause unwanted side effects(use at your own
                        risk)
  console_output_style (string):
                        console output: "classic", or with additional progress information ("progress" (percentage) |
                        "count").
  xfail_strict (bool):  default for the strict parameter of xfail markers when not given explicitly (default: False)
  enable_assertion_pass_hook (bool):
                        Enables the pytest_assertion_pass hook.Make sure to delete any previously generated pyc cache
                        files.
  junit_suite_name (string):
                        Test suite name for JUnit report
  junit_logging (string):
                        Write captured log messages to JUnit report: one of no|log|system-out|system-err|out-err|all
  junit_log_passing_tests (bool):
                        Capture log information for passing tests to JUnit report:
  junit_duration_report (string):
                        Duration time to report: one of total|call
  junit_family (string):
                        Emit XML for schema: one of legacy|xunit1|xunit2
  doctest_optionflags (args):
                        option flags for doctests
  doctest_encoding (string):
                        encoding used for doctest files
  cache_dir (string):   cache directory path.
  filterwarnings (linelist):
                        Each line specifies a pattern for warnings.filterwarnings. Processed after -W/--pythonwarnings.
  log_print (bool):     default value for --no-print-logs
  log_level (string):   default value for --log-level
  log_format (string):  default value for --log-format
  log_date_format (string):
                        default value for --log-date-format
  log_cli (bool):       enable log display during test run (also known as "live logging").
  log_cli_level (string):
                        default value for --log-cli-level
  log_cli_format (string):
                        default value for --log-cli-format
  log_cli_date_format (string):
                        default value for --log-cli-date-format
  log_file (string):    default value for --log-file
  log_file_level (string):
                        default value for --log-file-level
  log_file_format (string):
                        default value for --log-file-format
  log_file_date_format (string):
                        default value for --log-file-date-format
  log_auto_indent (string):
                        default value for --log-auto-indent
  faulthandler_timeout (string):
                        Dump the traceback of all threads if a test takes more than TIMEOUT seconds to finish. Not
                        available on Windows.
  addopts (args):       extra command line options
  minversion (string):  minimally required pytest version
  rsyncdirs (pathlist): list of (relative) paths to be rsynced for remote distributed testing.
  rsyncignore (pathlist):
                        list of (relative) glob-style paths to be ignored for rsyncing.
  looponfailroots (pathlist):
                        directories to check for changes

Where should pytest.ini be placed?
Just put it in the root directory of the project, don't mess around, don't mess with other names

Common configuration items

marks

Function: The @pytest.mark.webtest decorator is added to the test case. If the marks option is not added, warnings will be reported

Format: list list type
Writing method:

[pytest]
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict

Effect: Setting xfail_strict = True allows those test cases marked as @pytest.mark.xfail but actually passing XPASS to be reported as failed

Format: True, False (default), 1, 0

Writing:

[pytest]

# mark标记说明
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

Specific code example
When xfail_strict = True is not set, the test result shows XPASS

@pytest.mark.xfail()
def test_case1():
    a = "a"
    b = "b"
    assert a != b
collecting ... collected 1 item

02断言异常.py::test_case1 XPASS [100%]

============================= 1 xpassed in 0.02s ==============================

When xfail_strict = True is set, the test result shows failed

collecting ... collected 1 item

02断言异常.py::test_case1 FAILED                                         [100%]
02断言异常.py:54 (test_case1)
[XPASS(strict)] 

================================== FAILURES ===================================
_________________________________ test_case1 __________________________________
[XPASS(strict)] 
=========================== short test summary info ===========================
FAILED 02断言异常.py::test_case1
============================== 1 failed in 0.02s ==============================

addopts

Function: The addopts parameter can change the default command line options. When we enter a bunch of instructions in cmd to execute the use case, we can use this parameter instead, saving the repetitive work of typing commands

For example: if you want to test and generate a report, if you fail, rerun it twice, run it twice in total, and test it through distribution. If you write it in cmd, the command will be very long

pytest -v --rerun=2 --count=2 --html=report.html --self-contained-html -n=auto

It is not realistic to knock like this every time, addopts can perfectly solve this problem

[pytest]

# mark
markers =
    weibo: this is weibo page
    toutiao: toutiao
    xinlang: xinlang

xfail_strict = True

# 命令行参数
addopts = -v --reruns=1 --count=2 --html=reports.html --self-contained-html -n=auto

After adding addopts, we only need to type pytest in cmd to take effect.

norecursedirs

Function: When pytest collects test cases, it will recursively traverse all subdirectories, including some directories that you know you don't need to traverse. In this case, you can use the norecursedirs parameter to simplify the search work of pytest.

default setting:

norecursedirs = .* build dist CVS _darcs {
    
    arch} *.egg 

Correct writing: Multiple paths are separated by spaces

[pytest]

norecursedirs = .* build dist CVS _darcs {
    
    arch} *.egg venv src resources log report util

Change test case collection rules:

pytest’s default test case collection rules
File names start with test_*.py files and * test.py
functions starting
with test Classes starting with Test cannot contain init methods Methods
in classes starting with test_

We can modify or add this use case collection rule; of course, it is recommended to add it to the original rule, as follows

[pytest]

python_files =     test_*  *_test  test*
python_classes =   Test*   test*
python_functions = test_*  test*
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)

Only by constantly surpassing one's own courage can one shine brightly on the road of chasing dreams; only by persistent efforts can one create one's own brilliance. Believe in yourself, embrace every challenge, keep struggling, and the future will be magnificent!

Only through wind and rain can you see a rainbow; only by sharpening yourself can you become a diamond. Persist in struggle, go forward bravely, and in the journey of chasing dreams, every step is a catalyst for success. Believe in yourself, you can overcome all difficulties and achieve an extraordinary life!

Only by doing our best can we see the light of the future; only by persevering can we reach the other side of success; only by daring to struggle can we create our own brilliance. Believe in yourself, go forward bravely, you are invincible!

Guess you like

Origin blog.csdn.net/m0_70102063/article/details/131477722