From: http://www.testclass.net/pytest/multiple_tests/

背景

在现实的测试活动中,我们经常会定义多个用例文件,用来实现业务逻辑或其他逻辑上用例的物理分隔,比如

login_test.py # 登录相关功能的测试用例
cart_test.py # 购物车相关功能的测试用例
checkout_test.py # 结算相关功能的用例
order_test.py # 订单相关功能的用例

使用pytest可以很方便的执行批量执行一组文件中定义的用例。

潜规则

在批量执行用例之前,我们需要了解一下pytest的潜规则,注意,由于pytest可以支持丰富的定制选项,下面的潜规则是在没有定制的默认情况下的缺省规则

  • pytest会找当前以及递查找子文件夹下面所有的test_*.py*_test.py的文件,把其当作测试文件
  • 在这些文件里,pytest会收集下面的一些函数或方法,当作测试用例
    • 不在类定义中的以test_开头的函数或方法
    • 在以Test开头的类中(不能包含__init__方法),以test_开头的方法
  • pytest也支持unittest模式的用例定义

实例

新建test_calc.py文件,与上一节的test_quick_start.py放在同一文件夹下,敲入下面的内容

def add(x, y):
return x + y def test_add():
assert add(1, 0) == 1
assert add(1, 1) == 2
assert add(1, 99) == 100

现在当前文件夹下应该有2个文件

test_calc.py
test_quick_start.py

运行

在当前文件夹敲下面的命令

pytest

结果大致应该如下所示

$ pytest
========================================================================= test session starts =========================================================================
platform darwin -- Python 2.7.12, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /Users/easonhan/code/testclass.net/src/pytest, inifile:
collected 2 items test_calc.py .
test_quick_start.py . ====================================================================== 2 passed in 0.01 seconds =======================================================================

总结

上面的例子里

  • 所有以test_开头的py文件都被当成了测试文件
  • 所有测试文件中以test开头的方法被当成了测试用例执行

pytest.2.运行多个文件的更多相关文章

  1. jenkins+pytest+ allure运行多个py文件测试用例

    jenkins的pytest运行多个py文件,导出allure报告方法,只需改下job的配置中的构建即可(pytest会运行指定文件下的所有test开头的py文件),如下:              ...

  2. 【pytest】(三) pytest运行多个文件

    1.运行多个测试文件 pytest 会运行 test_ 开头 或者 _test 结尾的文件,在当前目录和子目录中 2. 一个类下的多个用例的运行, pytest会找到 test_ 开头的方法 impo ...

  3. pycharm中不以pytest方式运行,以普通方式运行的方法

    问题: 在调试一个很简单的代码时无法输出结果,运行结果如下形式: 出现以上情况,主要是因为在运行时是以pytest方式运行的,要换回以普通方式去运行. 问题解决: 首先是红色圈圈里的白色三角形. 然后 ...

  4. pytest-pyppeteer:在pytest中运行pyppeteer

    pytest-pyppeteer pytest-pyppeteer是我写的一个 pytest 插件,支持在 pytest 中运行pyppeteer,起因是为了解决工作中的一个测试需求,现在将其开源并做 ...

  5. 关于SQL Server 安装程序在运行 Windows Installer 文件时遇到错误

    前几日安装sql server2008r2 的时候碰到这个问题: 出现以下错误: SQL Server 安装程序在运行 Windows Installer 文件时遇到错误. Windows Insta ...

  6. IAR编译ZStack-CC2530为可下载运行的HEX文件的正确配置

    转自IAR编译ZStack-CC2530为可下载运行的HEX文件的正确配置 IAR编译ZStack-CC2530为可下载运行的HEX文件的正确配置:        1.正确配置输出文件格式:菜单选择P ...

  7. 如何使用AdvancedInstaller在安装包中运行一个.bat文件

    原文:如何使用AdvancedInstaller在安装包中运行一个.bat文件 1,  首先要保证你的Files and Folders模块下的Application Folder文件夹下包含你要运行 ...

  8. Django 在Django项目里单独运行某个py文件

    Python文件开头写以下代码: import os import django # 在environ字典里设置默认Django环境,'xxxx.settings'指Django项目的配置文件 os. ...

  9. 运行 python *.py 文件出错,如:python a.py

    运行 python *.py 文件出错,如:python a.py(下图) 原因:没有安装web.py 解决:下载并安装 网址:http://webpy.org/install#install  (h ...

随机推荐

  1. systemd设置静态IP

    /********************************************************************************* * systemd设置静态IP * ...

  2. Twisted 安装

    1,官网: https://www.twistedmatrix.com/trac/ 2,下载 https://twistedmatrix.com/Releases/Twisted/18.7/Twist ...

  3. $interpolateProvider

    angular.module('emailParser', []) .config(['$interpolateProvider', function($interpolateProvider) { ...

  4. [LeetCode&Python] Problem 13. Roman to Integer

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  5. oracle 数据库相关名词--图解

     通过下图,我们可以更好的理解oracle的结构关系. 知识拓展: 知识点及常用的命令如下: 1)通常情况我们称的“数据库”,并不仅指物理的数据集合,他包含物理数据.数据库管理系统.也即物理数据.内存 ...

  6. python str使用笔记(更新)

    判断字符串是否以某个串为结尾: str.endswith(strtmp) 返回True/False >>> strs='aba' >>> strs.endswith ...

  7. <--------------------------构造方法------------------------------>

    1 构造方法 初始化阶段 给对象的属性进行赋值 构造方法 什么是构造方法 : 字面 方法构建时 就使用的方法 对象创建的时候就使用的方法 作用:对象的属性值初始化2 如何用构造方法 修饰符 构造方法名 ...

  8. MySQL Binlog--MIXED模式下数据更新

    在 Mixed 模式下,MySQL 会根据执行的每一条具体的 SQL 语句来区分对待记录的日志形式,也就是在 statement 和 row 之间选择一种.如果SQL语句为UPDATE/DELETE等 ...

  9. Announcing HashiCorp Consul + Kubernetes

    转自:https://www.hashicorp.com/blog/consul-plus-kubernetes We're excited to announce multiple features ...

  10. Singer 学习九 运行&&开发taps、targets (四 开发target)

    singer 的target 需要从stdin 的行数据,同时处理schema.record.state 消息 指南 schema 需要进行关联stream records 数据的校验 一旦Targe ...