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. css3卡片阴影效果

    1.css3阴影用到的知识点:阴影box-shadow和插入:after before HTML部分: <!DOCTYPE html> <html> <head> ...

  2. 如何将scratch3.0的作业自动提交到后台数据库

    大家都知道Scratch3.0开发后,默认是可以下载文件到电脑,但是如果是作为商业系统来说,我们需要将作业自动的提交到后台,因此有了这篇文章. 首先,我们来分解下开发步骤: 1.在菜单栏新增一个上传到 ...

  3. 动态管理upsteam---nginx_http_dyups_module

    nginx_http_dyups_module  nginx_http_dyups_module是第三方开源软件,它提供API动态修改upstream的配置,并且支持Nginx的ip_hash.kee ...

  4. 运维笔试Python编程题

    一.用Python语言把列表[1,3,5,7,9]倒序并将元素变为字符类型,请写出多种方法: 第一种方法: list = [1, 3, 5, 7, 9] list.reverse() list2 = ...

  5. 【深入ASP.NET原理系列】--Asp.Net Mvc和Asp.Net WebForm实际上共用一套ASP.NET请求管道

    .NET FrameWork4在系统全局配置文件(如在如下目录中C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config) 中添加了一个名字叫Url ...

  6. go 学习 (一):环境配置

    Go 下载地址:https://golang.google.cn/dl/ 右键我的电脑  --> 左上方 “高级系统设置”   ---> 环境变量  -->  第二个菜单栏 “系统变 ...

  7. L1434滑雪

    一,看题 1,这个长度怎么算的. 从它自己数,可以走下去的位置. 2,这个题的衣服怎么披上去呀. 3,搜索目标,状态. 肯定要用坐标,不然怎么搜索. 4,在前期还是多写把. 5,我靠这个点还是随机的& ...

  8. am335x system upgrade kernel ethernet(四)

    1      Scope of Document This document describes ethernet hardware design and porting KZS8081 to ubo ...

  9. python(二)——if条件语句与基本数据类型

    if语句 缩进要保持一致 if 1 == 1: print('hello') if 2 == 2: print('world') else: print('python') elif inp = in ...

  10. Codeforces 526C.Om Nom and Candies

    题目描述 一个只有两个物品的背包问题,但是范围都是1e9,需要考虑根号或者log的复杂度. 如果这两个物品中的某一个花费超过了根号C,那么我们可以直接枚举这件物品的数量,另一件物品的数量可以计算得出. ...