1.Pytest测试用例运行规则


在pytest单元测试框架下面执行用例,需要满足以下几个特点:

  1. 文件名以test_*.py开头或者*_test.py

  2. 测试类、测试函数以test开头

  3. 所有的包必须要有 __init__.py文件

一般在cmd命令行下面执行pytest用例有3种方法。大家可以选择使用,我推荐第一种:

  pytest  文件名

  py.test  文件名

  python -m pytest 文件名

如果运行某个测试类下面的具体函数,可以使用:pytest  文件名::测试函数名

如果在测试过程中,遇到测试停止的方法可以加 -x参数: pytests -x 文件名

E:\untitled1>pytest -x collect.py
============================= test session starts =============================
platform win32 -- Python 3.5., pytest-5.0., py-1.8., pluggy-0.12.
rootdir: E:\untitled1
collected items collect.py .F [%] ================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x00000000036A4A58> def test_two(self):
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check') collect.py:: AssertionError
===================== failed, passed in 0.08 seconds ======================

从结果可以看出第二个测试用例没有运行成功并且停止了。当错误数达到某个量级时,测试停止,参数为:maxfail=num 示例如下:

E:\untitled1>pytest --maxfail= collect.py
============================= test session starts =============================
platform win32 -- Python 3.5., pytest-5.0., py-1.8., pluggy-0.12.
rootdir: E:\untitled1
collected items collect.py .F ================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x000000000368FCF8> def test_two(self):
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check') collect.py:: AssertionError
===================== failed, passed in 0.08 seconds ======================

2.在Pycharm中编写测试代码


在编写测试代码运行之前需要把切换pytest运行环境。file-->setting-->tools-->python intergrated tools--->default test runner 切换为 py.test  然后编写如下测试代码:

import pytest

class TestClass:

    def test_one(self):
x = 'hello'
assert 'h' in x def test_two(self):
x = 'hello'
assert hasattr(x, 'check') def test_secound(self):
assert 'x' in 'ddd' if __name__ == '__main__':
pytest.main('-q test_class.py')

运行结果如下:   (其中:     .表示测试结果是通过的 pass  E:表示errror  脚本中可能存在问题  F表示failed 测试结果不通过)

C:\Python35\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 5.0.3\helpers\pycharm\pytestrunner.py" -p pytest_teamcity E:/untitled1/test_class.py "-k TestClass and test_secound"
Testing started at : ...
============================= test session starts =============================
platform win32 -- Python 3.5., pytest-5.0., py-1.8., pluggy-0.12.
rootdir: C:\Program Files (x86)\JetBrains\PyCharm 5.0.\jre\jre\bin
collected items / deselected / selected . F
self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self):
> assert 'x' in 'ddd'
E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:: AssertionError ================================== FAILURES ===================================
___________________________ TestClass.test_secound ____________________________ self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self):
> assert 'x' in 'ddd'
E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:: AssertionError
=================== failed, deselected in 0.06 seconds ====================

Pytest单元测试框架-测试用例运行规则的更多相关文章

  1. Pytest单元测试框架-Pytest环境安装

    unittest是python自带的单元测试框架,它封装好了一些校验返回的结果方法和一些用例执行前的初始化操作,使得单元测试易于开展,因为它的易用性,很多同学也拿它来做功能测试和接口测试,只需简单开发 ...

  2. Pytest单元测试框架:插件-allure-pytest环境搭建并在本地生成一个测试报告

    之前写了allure-pytest的官方文档啃的内容,有些交流的朋友,实践起来没什么头绪,所以就有了这篇文章,也给自己填个坑 第一步:搭建Allure.JDK环境 1. 搭建JDK环境 不装jdk你会 ...

  3. Pytest单元测试框架之简单操作示例

    前言: Pytest是第三方单元格测试框架,更加简单,灵活,而且提供了更多丰富的扩展: Pytest与UnitTest框架的区别 UnitTest测试用例执行顺序是依照ascii码执行,而Pytest ...

  4. Pytest单元测试框架-学习

    pytest: Python的一个单元测试框架,基于UnitTest二次开发,语法上更加简洁,可以用来做Python开发项目的单元测试,UI自动化.接口自动化测试等,有很多的插件访问Pytest插件汇 ...

  5. pytest单元测试框架

    一.安装方式 1.安装命令:pip install pytest 2.html安装插件:pip install pytest -html 二.pytest执行指定测试用例 1.思想:通过对测试用例进行 ...

  6. Pytest 单元测试框架

    1.pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效 2.安装 pytest pip install pytest 3.验证 pytest 是否安装成功 p ...

  7. Pytest单元测试框架——Pytest+Allure+Jenkins的应用

    一.简介 pytest+allure+jenkins进行接口测试.生成测试报告.结合jenkins进行集成. pytest是python的一种单元测试框架,与python自带的unittest测试框架 ...

  8. Pytest单元测试框架之FixTure基本使用

    前言: 在单元测试框架中,主要分为:测试固件,测试用例,测试套件,测试执行及测试报告: 测试固件不难理解,也就是我们在执行测试用例前需要做的动作和测试执行后的需要做的事情: 比如在UI自动化测试中,我 ...

  9. Pytest单元测试框架-allure测试报告

    Allure Test Report 对于不同的编程语言,有很多很酷的测试框架.不幸的是,它们中只有少数能够提供测试执行输出的良好表示.Qameta软件测试团队正在致力于Allure--一个开源框架, ...

随机推荐

  1. Java输入流

    import java.util.*;  //java为小写public class TEST{ public static void main(String args[]){ Scanner inp ...

  2. InnoDB存储引擎与MyIsam存储引擎的区别

    特性比较 mysql5.5之后默认的存储引擎为InnoDB,在此之前默认存储引擎是MyIsam 特点 MyIsam InnoDB 锁机制 表锁 行锁 事务 不支持 支持 外键 不支持 支持 B树索引 ...

  3. tkinter改进了随机显示图片

    随机显示,还加了圆圈,这样感觉更好点. from django.test import TestCase # Create your tests here. import random import ...

  4. 项目笔记---WPF之Metro风格UI(转)

    写在前面 作为新年开篇的文章,当然要选择比较“Cool”的东西来分享,这自然落到了WPF身上,WPF技术自身可塑性非常强,其强大的绘图技术以及XAML技术比WinForm而言有本质的飞跃. 切入正题, ...

  5. ARTS-week6

    Algorithm 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2 Tw ...

  6. Docker创建mysql镜像

    原文: https://blog.csdn.net/uk8692/article/details/49386679 https://blog.csdn.net/qq362228416/article/ ...

  7. 合并K个有序链表

    方法一:采用归并的思想将链表两两合并,再将两两合并后的链表做同样的操作直到合并为只有一个新的链表为止. 归类的时间复杂度是O(logn),合并两个链表的时间复杂度是O(n),则总的时间复杂度大概是O( ...

  8. GlusterFS集群文件系统研究

    https://blog.csdn.net/liuaigui/article/details/6284551 1.      GlusterFS概述GlusterFS是Scale-Out存储解决方案G ...

  9. make 命令出现:"make:*** No targets specified and no makefile found.Stop."

    我们在Linux 安装包的时候,使用make 命令出现:"make:*** No targets specified and no makefile found.Stop."这样的 ...

  10. springboot(二)

    SpringBoot使用JSP 1.创建一个webapp子项目 2.导入依赖 <!-- 配置springboot的父节点依赖,之后引入就不需要添加version配置了! springboot会自 ...