python interface to automate 11-pytest entry

Foreword

pytest is a very mature full-featured Python testing framework for unit from simple to complex functional tests, the main features are the following:

  • Simple, flexible, easy to use;
  • Supports parameterized;
  • Simple tests can support unit;
  • Test function and attribute mark
  • Complex functional tests, such as selenium, etc. can be done automated testing, test automation interfaces (pytest + requests);
  • pytest with many third-party plug-ins, and can be custom extensions, such as better quality pytest-selenium (integrated selenium), pytest-html (perfect html report generator) and the like;
  • Skip and xfail: unsuccessful test processing;
  • It can be very good and jenkins integration;
  • Distributed by xdist test plug-in to multiple CPU

I. Introduction

1, recommended version environment to build match: pip install pytest == 3.6.3

  • Python3.6.x + pytest 3.6.3
  • Python3.7.x + pytest 4.0.2

2, view the version: pytest --version

C:\Users\Administrator>pytest --version
This is pytest version 3、6、3, imported from d:\path_python\lib\site-packages\pytest、py

3, pytest naming rules:

  • Filename test _ *, py or * _test, py
  • * Test class is beginning
  • Function / method begins with test_ *

4, pytest directly written embodiment, cmd finished running, no import other modules.

G: \ python_study \ Study \ pytest_demo \ Study> pytest - S test_demo1.py
 ================================= ================ test session starts =============================== ================== 
Platform Win32 - the Python 3.6.5, 3.6.3-pytest, Py-1.8.0, pluggy 0.6- .0 
RootDir: G: \ python_study \ Study \ pytest_demo \ Study, inifile: 
Collected 2 items 
test_demo1.py I use case: A . I use case: b .
======================= 2 passed ======================= in 0.02 seconds The ====================== =========================

Two, pytest command-line parameters introduced

1, operating rules: pytest py file path

C:\Users\Administrator>pytest G:\python_study\study\pytest_demo\study\test_demo.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: C:\Users\Administrator, inifile:
collected 4 items

test_demo.py ....                                                        [100%]

========================== 4 passed in 0.03 seconds ===========================

2, display printing information (or will not see print content): pytest -s xxx

G: \ python_study \ Study \ pytest_demo \ Study> pytest - S test_demo1.py
 ================================= ================ test session starts =============================== ================== 
Platform Win32 - the Python 3.6.5, 3.6.3-pytest, Py-1.8.0, pluggy 0.6- .0 
RootDir: G: \ python_study \ Study \ pytest_demo \ Study, inifile: 
Collected 2 items 

test_demo1.py 
I use case: A 
. 
I use case: b 
.

 ======================= 2 passed ======================= in 0.02 seconds The ====================== =========================

3 Show Details: pytest -v xxx

G:\python_study\study\pytest_demo\study>pytest test_demo1.py -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items

test_demo1.py::Test_api::test_a PASSED                                   [ 50%]
test_demo1.py::Test_api::test_b PASSED                                   [100%]

========================== 2 passed in 0.02 seconds ===========================

4, simple display information: pytest -q xxx

G:\python_study\study\pytest_demo\study>pytest test_demo1.py -q
..                                                                       [100%]
2 passed in 0.02 seconds

5, with the run designated Example: pytest -k case_name (case_name function can be based, fuzzy match keywords), the following matching demo

G:\python_study\study\pytest_demo\study>pytest -k demo -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 11 items / 5 deselected

test_demo.py::test_ab PASSED                                             [ 16%]
test_demo.py::test_aba PASSED                                            [ 33%]
test_demo.py::Test_api::test_aa PASSED                                   [ 50%]
test_demo.py::Test_api::test_b PASSED                                    [ 66%]
test_demo1.py::Test_api::test_a PASSED                                   [ 83%]
test_demo1.py::Test_api::test_b PASSED                                   [100%]

=================== 6 passed, 5 deselected in 0.06 seconds ====================

6, command line parameters, regardless of the order, as well as other command line parameters, not anything in detail:

  • Run like the use case and does not run like a use case: pytest -v -k "Test_api1 and not test_a"
  • Failed to stop the test: pytest -x
  • The test was stopped after a specified number of failed: pytest --maxfail = 2
  • Example running with a failure (or no failure): pytest --last-failed
  • and many more

Please see pytest -h or find a degree of your mother, we generally use the above parameters enough daily use. Welcome to QQ exchange group: 482 713 805

Guess you like

Origin www.cnblogs.com/gsxl/p/12051272.html