• pytest入门


    pytest入门

    一、安装pytest

    pip install pytest
    

    二、运行pytest

    pytest [options] [file_or_dir] [file_or_dir] [...]
    

    如果不提供任何参数,pytest会在当前目录以及子目录下寻找测试文件,然后运行测试用例,如果提供了文件名或目录名,pytest会诸葛查找并运行所有的测试用例。

    运行规则

    • 文件名以test_.py文件和_test.py
    • 以test_开头的函数
    • 以Test开头的类
    • 以test_开头的方法
    • 所有的包pakege必须要有__init__.py文件

    运行

    目录结构

    .
    ├── __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 ====================================================
      
    2. 指定类中的指定方法

      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 ====================================================
      
    3. 指定函数

      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 ====================================================
      

    运行结果

    • PASSED(.):测试通过
    • FAILED(F):测试失败(也有可能时XPASS状态和strict选项冲突造成的失败)
    • SKIPPED(s):测试未被执行。指定测试跳过执行,可以将测试用例标记为@pytest.mark.skip(),或这@pytest.mark.skipif()
    • xfail(x):预期测试失败,并且确实失败。使用@pytest.mark.xfail()标记指定认为回失败的测试用例
    • ERROR(E):测试用例之外的代码触发了异常,可能由fixture引起,也可能由hook函数引起。
  • 相关阅读:
    python依赖包整体迁移方法
    ubuntu关于ssh协议登录问题
    k8s部署02-----kubeadm部署k8s
    k8s部署01-----what is k8s?
    兼容到ie10的js文件导出、下载到本地
    webstorm减少内存占用
    foxmail占cpu 100%解决办法
    原生js返回顶部
    js字符串驼峰和下划线互相转换
    element-ui的rules中正则表达式
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13258137.html
Copyright © 2020-2023  润新知