zoukankan      html  css  js  c++  java
  • 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函数引起。
  • 相关阅读:
    Roce ofed 环境搭建与测试
    Ubuntu 1804 搭建NFS服务器
    Redhat 8.0.0 安装与网络配置
    Centos 8.1 安装与网络配置
    SUSE 15.1 系统安装
    VSpare ESXi 7.0 基本使用(模板、iso、SRIOV)
    VSpare ESXi 7.0 服务器安装
    open SUSE leap 15.1 安装图解
    KVM虚拟机网卡连接网桥
    GitHub Action一键部署配置,值得拥有
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13258137.html
Copyright © 2011-2022 走看看