关于pytest的一些问题
一. 测试模块内部使用fixture和测试模块调用外部公共的fixture
1. unittest框架下的测试用例模块
from selenium import webdriver
import unittest
from PageObjects.login_page import LoginPage
from PageObjects.index_page import IndexPage
from TestDatas.login_testdatas import *
from TestDatas.CommonData import *
import time
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get(common_url)
self.driver.maximize_window()
#用户名密码正确
def test_login_success(self):
LoginPage(self.driver).login(login_success_data["username"], login_success_data["passwd"])
nickName = IndexPage(self.driver).get_nickName()
self.assertEqual("我的帐户[小蜜蜂96027]", nickName)
def tearDown(self):
time.sleep(2)
self.driver.quit()
2. 测试用例模块内部使用fixture
fixture:即测试用例执行的环境准备和清理,在unittest中即指setup()、teardown()
from selenium import webdriver
import unittest
from PageObjects.login_page import LoginPage
from PageObjects.index_page import IndexPage
from TestDatas.login_testdatas import *
from TestDatas.CommonData import *
import time
import pytest
class TestLogin:
@pytest.fixture
def init_driver(self):
self.driver = webdriver.Chrome()
self.driver.get(common_url)
self.driver.maximize_window()
yield self.driver
time.sleep(2)
self.driver.quit()
#用户名密码正确
def test_login_success(self, init_driver):
LoginPage(init_driver).login(login_success_data["username"], login_success_data["passwd"])
nickName = IndexPage(init_driver).get_nickName()
assert "我的帐户[小蜜蜂96027]" == nickName
3. 测试用例模块调用外部公共的fixture
有时,多个测试用例模块要使用同一fixture,如果在每个模块内部都定义一遍fixture则略显麻烦,pytest提供了conftest.py文件,用来定义公共的fixture,多个测试模块都可以调用,运行测试用例时,不需要导入这个文件,它会自动查找conftest.py文件,然后去找到对应的fixture
在测试用例模块的同级目录下创建一个conftest.py文件
from selenium import webdriver
from TestDatas.CommonData import *
import pytest
import time
@pytest.fixture
def init_driver():
driver = webdriver.Chrome()
driver.get(common_url)
driver.maximize_window()
yield driver
time.sleep(2)
driver.quit()
测试用例模块test_login.py调用外部公共的fixture
from selenium import webdriver
import unittest
from PageObjects.login_page import LoginPage
from PageObjects.index_page import IndexPage
from TestDatas.login_testdatas import *
from TestDatas.CommonData import *
import time
import pytest
class TestLogin:
#用户名密码正确
@pytest.mark.usefixtures("init_driver")
def test_login_success(self, init_driver):
LoginPage(init_driver).login(login_success_data["username"], login_success_data["passwd"])
nickName = IndexPage(init_driver).get_nickName()
assert "我的帐户[小蜜蜂96027]" == nickName
4. 比较
pytest和unittest相比:
1. 需要import pytest
2. 不再需要unittest.TestCase
3. 如果在内部使用fixture,则函数只需要传参,参数为被@pytest.fixture装饰的函数名init_driver(init_driver=init_driver());如果调用外部公共的fixture,除了传参之外,还要在测试用例类或函数前面加上一句:@pytest.mark.usefixtures("init_driver")
4. fixture函数中以yield分割环境准备和环境清理,且需要返回self.driver或driver
5. 断言方式有所改变,unittest采用self.assertEqual或其他,pytest采用assert 表达式
二. 装饰器函数fixture的作用域默认是function,且autouse=False
@pytest.fixture中的fixture函数源码如下

四大作用域:
function:每个test都运行,默认是function的scope,即unittest中的setUp和tearDown
class:每个class的所有test只运行一次。即unittest中的setUpClass和tearDownClass
module:每个module的所有test只运行一次(分类里和类外)
session:每个session只运行一次(如连接数据库操作)
值得注意的是默认的autouse=False,所以函数需要使用对应的fixture时,需要添加@pytest.mark.usefixtures("init_driver"),并且将被fixture装饰的函数名init_driver作为参数,传给测试用例函数,如果autouse=True,那么会自动给每个测试用例添加fixture,而init_driver这样的环境准备和清理工作并非每个测试用例都需要,所以autouse一般采用默认值False
三. pytest收集测试用例的规则
1. 默认从当前目录中搜集测试用例,即在哪个目录下运行pytest命令,则从哪个目录当中搜索
2. 搜索规则:
1) 符合命令规则test_*.py 或者 *_test.py 的文件
2) 以test_开头的函数名
3) 以Test开头的测试类 (没有__init__函数) 当中,以test_开头的函数
当前目录下(D:\python_workshop\WEB_framework)有两个测试用例模块,test_login.py和test_invest.py,前者测试用例类TestLogin中有四个测试用例,另外还在测试用例类外添加了一个test_demo函数,后者有四个测试用例,这九个测试用例中我只在用户名为空的用例上打了smoke标记

在调用pytest -m smoke运行时,发现9条用例被找到了,但8条没有被选择,只选择了打了标记的

参考文章
http://www.cnblogs.com/cnhkzyy/p/9270830.html
关于pytest的一些问题的更多相关文章
- flask-sqlalchemy、pytest 的单元测试和事务自动回滚
flask-sqlalchemy.pytest 的单元测试和事务自动回滚 使用 flask-sqlalchemy 做数据库时,单元测试可以帮助发现一些可能意想不到的问题,像 delete-cascad ...
- Python单元测试框架之pytest -- 断言
对于测试来讲,不管是功能测试,自动化测试,还是单元测试.一般都会预设一个正确的预期结果,而在测试执行的过程中会得到一个实际的结果.测试的成功与否就是拿实际的结果与预期的结果进行比较.这个比的过程实际就 ...
- Python单元测试框架之pytest -- fixtures
fixtures不太好翻译,可看作是夹心饼干最外层的两片饼干.通常用setup/teardown来表示.它主要用来包裹测试用例,为什么需要这样的饼干呢?我们以web自动化测试为例,例如,要测试的某系统 ...
- Python单元测试框架之pytest -- 生成测试报告
继续pytest单元测试框架的学习,pytest可以生成多种类型的测试报告.这一节就来学习pytest如何生成测试报告. 创建test_calss.py 测试用例文件,这里以测试该文件为例. #cod ...
- 使用 tox flake8 pytest 规范 python 项目
使用 tox flake8 pytest 规范 python 项目 python 中有些很好的工作来规范整个项目的开发,而其中使用较多的就是使用 tox . flake8 . pytest . tox ...
- pytest学习笔记(三)
接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...
- pytest学习笔记(二)
继续文档的第二章 (一)pytest中可以在命令行中静态/动态添加option,这里没什么好讲的,略过... 这里面主要讲下如何试用skip/xfail,还有incremental(包含一些列的测试步 ...
- pytest进阶之配置文件
前言 pytest配置文件能够改变pytest框架代码的运行规则.比如修改pytest收集用例的规则,添加命令行参数等等!下面我们来一一讲解常用的一些配置项 Help 通过命令pytest --hel ...
- pytest进阶之html测试报告
前言 Pytest系列已经写了几篇文章了,也不知道对多少人有帮助,总之对于我自己来说该掌握的都已经掌握了,那么今天我们再来说说pytest如何生成一个完整的html测试报告,让你在吹牛逼的路上再多一份 ...
- pytest进阶之xunit fixture
前言 今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearD ...
随机推荐
- IO流入门-概述
纲要 Java流概述 文件流 缓冲流 转换流 打印流 对象流 File类 流的概念 按方向划分:输入流和输出流,是相对内存而言的.从内存出来是输出,到内存中就是输入.输入流又叫做InputStream ...
- JavaScript四种数值取整方法
一.Math.trunc() 1.定义 Math.trunc()方法去除数字的小数部分,保留整数部分. 2.语法 Math.trunc(value) 3.示例 console.log(Math.tru ...
- logging/re - 总结
logging 模块 很多程序都有记录日志的需求 logging的日志可以分为 debug(), info(), warning(), error() and critical()5个级别 1.输出到 ...
- git rm与直接rm的区别
git rm 行为: 1.删除一个文件 2.将被删除的这个文件纳入缓存区 $ git rm a rm 'a' $ git status On branch master Changes to be c ...
- tensorflow 中 softmax_cross_entropy_with_logits 与 sparse_softmax_cross_entropy_with_logits 的区别
http://stackoverflow.com/questions/37312421/tensorflow-whats-the-difference-between-sparse-softmax-c ...
- python并发编程之IO模型(Day38)
一.IO模型介绍 为了更好的学习IO模型,可以先看同步,异步,阻塞,非阻塞 http://www.cnblogs.com/linhaifeng/articles/7430066.html#_label ...
- Linux常用命令(更新)
- (转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
from:http://www.cnblogs.com/Gildor/archive/2010/12/13/1904060.html HttpWebRequest以UTF-8编码写入内容时发生“Byt ...
- Tomcat 源码分析(转)
本文转自:http://blog.csdn.net/haitao111313/article/category/1179996 Tomcat源码分析(一)--服务启动 1. Tomcat主要有两个组件 ...
- 【leetcode刷题笔记】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...