首页 > 其他 > 详细

命令行执行

时间:2020-06-08 09:26:53      阅读:33      评论:0      收藏:0      [点我收藏+]

命令行执行

目录结构

.
├── __init__,py
├── test_001.py
└── test_002.py

test_001.py

#!/usr/bin/python3
#-*- conding:utf-8 -*-

def test_one():
    print(‘test_one‘)

def test_two():
    print(‘test_two‘)

class Test_Class():
    def test_three(self):
        print(‘test_three‘)

test_2.py

#!/usr/bin/python3
#-*- conding:utf-8 -*-

def test_1():
    print(‘test_1‘)

一、直接执行

  • 执行当前目录下所有的测试用例
pytest
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 4 items                                                                                                        

test_001.py ...                                                                                                    [ 75%]
test_002.py .                                                                                                      [100%]

=================================================== 4 passed in 0.02s ====================================================
  • 执行指定模块中的所有测试用例
pytest test_one.py
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items                                                                                                        

test_001.py ...                                                                                                    [100%]

=================================================== 3 passed in 0.02s ====================================================
  • 执行指定模块中的指定测试用例

    1. 指定类
    pytest test_001.py::Test_Class
    
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 1 item                                                                                                         
    
    test_001.py .                                                                                                      [100%]
    
    =================================================== 1 passed in 0.01s ====================================================
    
    1. 指定类中的指定方法
    pytest test_001.py::Test_Class::test_three
    
    ================================================== test session starts ===================================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 1 item                                                                                                         
    
    test_001.py .                                                                                                      [100%]
    
    =================================================== 1 passed in 0.01s ====================================================
    
    1. 指定函数
    pytest test_001.py::test_two
    
    ================================================== test session starts             ===================================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 1 item                                                                                                         
    
    test_001.py .                                                                                                      [100%]
    
    =================================================== 1 passed in 0.01s ====================================================
    

二、带参数执行

1、-v

可以输出用例详细的执行信息,比如用例所在的文件及用例名称

pytest -v
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 4 items                                                                                                        

test_001.py::test_one PASSED                                                                                       [ 25%]
test_001.py::test_two PASSED                                                                                       [ 50%]
test_001.py::Test_Class::test_three PASSED                                                                         [ 75%]
test_002.py::test_1 PASSED                                                                                         [100%]

=================================================== 4 passed in 0.02s ====================================================

2、-s

可以输出用例中的调试信息,比如print打印信息等

pytest -s test_002.py
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 1 item                                                                                                         

test_002.py test_1
.

=================================================== 1 passed in 0.01s ====================================================

3、-k

执行用例中包含“关键字”的用例

pytest -vk "three"
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 4 items / 3 deselected / 1 selected                                                                            

test_001.py::Test_Class::test_three PASSED                                                                         [100%]

============================================ 1 passed, 3 deselected in 0.01s =============================================

4、-m

执行带有特定标记(@pytest.mark.‘标记’)的测试用例
新增test_003.py

#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest


def test_01():
    print(‘test_01‘)

@pytest.mark.aaa
def test_02():
    print(‘test_02‘)

class Test_Class():
    @pytest.mark.aaa
    def test_03(self):
        print(‘test_03‘)
pytest -vm "aaa"
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 7 items / 5 deselected / 2 selected                                                                            

test_003.py::test_02 PASSED                                                                                        [ 50%]
test_003.py::Test_Class::test_03 PASSED                                                                            [100%]

==================================================== warnings summary ====================================================
test_003.py:9
  /media/_dde_data/python/test_003.py:9: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    @pytest.mark.aaa

test_003.py:14
  /media/_dde_data/python/test_003.py:14: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    @pytest.mark.aaa

-- Docs: https://docs.pytest.org/en/latest/warnings.html
====================================== 2 passed, 5 deselected, 2 warnings in 0.02s =======================================

5、-x

遇到错误时停止
新增test_004.py

#!/usr/bin/python3
#-*- conding:utf-8 -*-

def test_a():
    print("test_a")
    assert 1 == 3


def test_b():
    print(‘test_b‘)
    assert 2 == 4

def test_c():
    print(‘test_c‘)
    assert 3 == 3
pytest -x test_004.py
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items                                                                                                        

test_004.py F

======================================================== FAILURES ========================================================
_________________________________________________________ test_a _________________________________________________________

    def test_a():
        print("test_a")
>       assert 1 == 3
E       assert 1 == 3

test_004.py:6: AssertionError
-------------------------------------------------- Captured stdout call --------------------------------------------------
test_a
================================================ short test summary info =================================================
FAILED test_004.py::test_a - assert 1 == 3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=================================================== 1 failed in 0.17s ====================================================

6、--maxfail

pytest --maxfail=2 test_004.py
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items                                                                                                        

test_004.py FF

======================================================== FAILURES ========================================================
_________________________________________________________ test_a _________________________________________________________

    def test_a():
        print("test_a")
>       assert 1 == 3
E       assert 1 == 3

test_004.py:6: AssertionError
-------------------------------------------------- Captured stdout call --------------------------------------------------
test_a
_________________________________________________________ test_b _________________________________________________________

    def test_b():
        print(‘test_b‘)
>       assert 2 == 4
E       assert 2 == 4

test_004.py:11: AssertionError
-------------------------------------------------- Captured stdout call --------------------------------------------------
test_b
================================================ short test summary info =================================================
FAILED test_004.py::test_a - assert 1 == 3
FAILED test_004.py::test_b - assert 2 == 4
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=================================================== 2 failed in 0.17s ====================================================

7、-q

简化控制台的输出

pytest -q test_001.py 
...                                                                                                                [100%]
3 passed in 0.01s

8、--collect-only

罗列出当前目录下所有的测试模块,测试类及测试函数

pytest --collect-only
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 10 items                                                                                                       
<Module test_001.py>
  <Function test_one>
  <Function test_two>
  <Class Test_Class>
      <Function test_three>
<Module test_002.py>
  <Function test_1>
<Module test_003.py>
  <Function test_01>
  <Function test_02>
  <Class Test_Class>
      <Function test_03>
<Module test_004.py>
  <Function test_a>
  <Function test_b>
  <Function test_c>

==================================================== warnings summary ====================================================
test_003.py:9
  /media/_dde_data/python/test_003.py:9: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    @pytest.mark.aaa

test_003.py:14
  /media/_dde_data/python/test_003.py:14: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    @pytest.mark.aaa

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================================================== 2 warnings in 0.02s ===================================================

9、--lf

当一次用例执行完成后,如果其中存在失败的测试用例,那么我们可以使用此命令重新运行失败的测试用例

pytest  test_004.py
============================================== 2 failed, 1 passed in 0.18s ===============================================

2个失败一个成功

pytest  --lf

只会重新执行失败的2个用例

=================================================== 2 failed in 0.19s ====================================================

9、–tb=style

屏蔽测试用例输出的回溯信息,可以简化失败用例的输出信息。style可以时‘auto‘, ‘long‘, ‘short‘, ‘no‘, ‘line‘, ‘native

pytest  --tb=short test_004.py 
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items                                                                                                        

test_004.py FF.                                                                                                    [100%]

======================================================== FAILURES ========================================================
_________________________________________________________ test_a _________________________________________________________
test_004.py:6: in test_a
    assert 1 == 3
E   assert 1 == 3
-------------------------------------------------- Captured stdout call --------------------------------------------------
test_a
_________________________________________________________ test_b _________________________________________________________
test_004.py:11: in test_b
    assert 2 == 4
E   assert 2 == 4
-------------------------------------------------- Captured stdout call --------------------------------------------------
test_b
================================================ short test summary info =================================================
FAILED test_004.py::test_a - assert 1 == 3
FAILED test_004.py::test_b - assert 2 == 4
============================================== 2 failed, 1 passed in 0.19s ===============================================
pytest  --tb=no test_004.py 
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items                                                                                                        

test_004.py FF.                                                                                                    [100%]

================================================ short test summary info =================================================
FAILED test_004.py::test_a - assert 1 == 3
FAILED test_004.py::test_b - assert 2 == 4
============================================== 2 failed, 1 passed in 0.18s ===============================================
pytest  --tb=line test_004.py 
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items                                                                                                        

test_004.py FF.                                                                                                    [100%]

======================================================== FAILURES ========================================================
/media/_dde_data/python/test_004.py:6: assert 1 == 3
/media/_dde_data/python/test_004.py:11: assert 2 == 4
================================================ short test summary info =================================================
FAILED test_004.py::test_a - assert 1 == 3
FAILED test_004.py::test_b - assert 2 == 4
============================================== 2 failed, 1 passed in 0.18s ===============================================

命令行执行

原文:https://www.cnblogs.com/jingxindeyi/p/13063149.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!