zoukankan      html  css  js  c++  java
  • pytest--使用前提

     
    python 鄙视链:pytest 鄙视 > unittest 鄙视 >  robotframework 。
    pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效,支持315种以上的插件,同时兼容 unittest 框架。这就使得我们在 unittest 框架迁移到 pytest 框架的时候不需要重写代码。
     
    一、安装
    首先使用 pip 安装 pytest 
    pip3 install pytest
     查看 pytest 是否安装成功
    pytest --version
     
    二、简单使用
    1.创建 test_sample.py 文件,代码如下:
    #!/usr/bin/env python
    # coding=utf-8
    import pytest
    
    def inc(x):
        return x + 1
    
    def test_answer():
        assert inc(3) == 5
    
    if __name__ =="__main__":
        pytest.main()
    执行结果:
    test_sample.py F                                                         [100%]
    ================================== FAILURES ===================================
    _________________________________ test_answer _________________________________
        def test_answer():
    >       assert inc(3) == 5
    E       assert 4 == 5
    E        +  where 4 = inc(3)
    
    test_sample.py:19: AssertionError
    ============================== 1 failed in 0.41s ==============================
    从上面的例子可以看出,pytest 中断言的用法直接使用 assert ,和 unittest 中断言 self.assert 用法有所区别。
     
    2.总结一下:使用 pytest 执行测试需要遵行的规则:
    • .py 测试文件必须以test_开头(或者以_test结尾) 
    • 测试类必须以Test开头,并且不能有 init 方法
    • 测试方法必须以test_开头
    • 断言必须使用 assert
     
    三、文件结构
    project root folder
          |——…
          |——src
          |    |——…
          |——tests
          |    |——conftest.py
          |    |——pytest.ini
          |——func
          |    |——__init__.py
          |    |——test_xx.py
          |    |—— …
          |——unit
          |    |——__init__.py
          |    |——test_xx.py
          |    |——…
     

  • 相关阅读:
    PAT 1006 Sign In and Sign Out
    Winform 自定义程序安装向导(可用于数据库升级等)
    Winform 数据库连接配置界面
    SQLServer禁用、启用外键约束
    sp_MSforeachtable使用方法
    【.NET】使用HtmlAgilityPack抓取网页数据
    SQL:bat批处理多个.sql文件
    C#:数据库通用访问类 SqlHelper
    C#:最简洁强大的代码生成器
    SQL 分组后拼接字符串
  • 原文地址:https://www.cnblogs.com/absoluteli/p/13984492.html
Copyright © 2011-2022 走看看