pytest

Pytest【定制fixture】

有些话、适合烂在心里 提交于 2019-12-01 12:42:43
在pytest中的fixture是在测试函数运行前后,由pytest执行的外壳函数,fixture中的代码可以定制,满足多变的测试需求:包括定义传入测试中的数据集、配置测试前系统的初始化状态、为批量测试提供数据源。 import pytest @pytest.fixture() def return_data(): return 1000 def test_someting(return_data): assert return_data == 1000 执行结果如下: (venv) E:\Programs\Python\Python_Pytest\TestScripts>pytest -v test_fixture.py ============= test session starts ==================================== platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe cachedir: .pytest_cache rootdir: E:\Programs\Python\Python_Pytest\TestScripts plugins: xdist-1.29.0, timeout-1.3.3,

python自动化测试框架

折月煮酒 提交于 2019-12-01 12:34:53
一.环境准备   1.python开发环境, python3.7   2.setuptools基础工具包   3.pip安装包管理工具   4.selenium自动化测试工具   5.pytest自动化测试框架 二.pytest用法   1.命名规则 测试文件应该命名为test_.py或_test.py 测试方法和函数应该被命名为test_。 测试类应该被命名为Test   2.结果类型 PASSED (.):测试成功。 FAILED (F):测试失败(或XPASS + strict)。 SKIPPED (s): 测试被跳过。 你可以使用@pytest.mark.skip()或 pytest.mark.skipif()修饰器告诉pytest跳过测试 xfail (x):预期测试失败。@pytest.mark.xfail() XPASS (X):测试不应该通过。 ERROR (E):错误   3.执行方法 py.test -x : 首次失败后停止执行 py.test --maxfail=2 : 两次失败之后停止执行 py.test -k answer1 -v: 运行所有名字中含有的 answer1 的方法 , -k 用来匹配名字中包含表达式的方法, -v 增加显示详细信息 py.test -m <name>: 运行通过 marked 的用例, -m 标记的名字 @pytest

pytest 2.3 adding teardowns within the class

夙愿已清 提交于 2019-12-01 12:08:22
I'm researching new version of pytest (2.3) and getting very excited about the new functionality where you "can precisely control teardown by registering one or multiple teardown functions as soon as they have performed some actions which need undoing, eliminating the no need for a separate “teardown” decorator" from here It's all pretty clear when it's used as function, but how to use it in the class? class Test(object): @pytest.setup(scope='class') def stp(self): self.propty = "something" def test_something(self): ... # some code # need to add something to the teardown def test_something

Pytest命令行执行测试

こ雲淡風輕ζ 提交于 2019-12-01 11:59:43
Pytest命令行执行测试 from collections import namedtuple Task = namedtuple('Task', ['summary','owner','done','id']) # __new__.__defaults__创建默认的Task对象 Task.__new__.__defaults__ = (None, None, False, None) def test_default(): """ 如果不传任何参数,则默认调用缺省对象Task.__new__.__defaults__ = (None, None, False, None) """ t1 = Task() t2 = Task(None, None, False, None) assert t1 == t2 def test_member_access(): """ 利用属性名来访问对象成员 :return: """ t = Task('buy milk', 'brian') assert t.summary == 'buy milk' assert t.owner == 'brian' assert(t.done, t.id) == (False, None) def test_asdict(): """ _asdict()返回一个字典 """ t_task = Task('do

pytest 2.3 adding teardowns within the class

五迷三道 提交于 2019-12-01 11:07:47
问题 I'm researching new version of pytest (2.3) and getting very excited about the new functionality where you "can precisely control teardown by registering one or multiple teardown functions as soon as they have performed some actions which need undoing, eliminating the no need for a separate “teardown” decorator" from here It's all pretty clear when it's used as function, but how to use it in the class? class Test(object): @pytest.setup(scope='class') def stp(self): self.propty = "something"

Pytest权威教程22-优质集成实践

不羁的心 提交于 2019-12-01 09:59:26
目录 优质集成实践 使用pip安装包 Python测试发现的约定 选择测试布局结构/导入规则 在应用程序代码外测试 测试作为应用程序代码的一部分 tox 返回: Pytest权威教程 优质集成实践 使用pip安装包 对于开发,我们建议你将[venv来安装应用程序和任何依赖项,以及 pytest 包本身。这可确保你的代码和依赖项与系统Python安装隔离。 接下来, setup.py 使用以下最低内容将文件放在包的根目录中: from setuptools import setup,find_packages setup(name="PACKAGENAME",packages=find_packages()) PACKAGENAME 包裹的名称在哪里。然后,你可以通过从同一目录运行,以“可编辑”模式安装程序包: pip install -e . 它允许你更改源代码(测试和应用程序)并随意重新运行测试。这与运行类似,或者使用符号链接将你的包安装到开发代码中。 pythonsetup.pydevelop``condadevelop Python测试发现的约定 Pytest 实现以下标准测试发现: 如果未指定参数,则从`testpaths(如果已配置)或当前目录开始收集。或者,命令行参数可以用于目录,文件名或节点ID的任意组合。 递归到目录,除非它们匹配 norecursedirs 。

Pytest权威教程23-片状测试

被刻印的时光 ゝ 提交于 2019-12-01 09:59:17
目录 片状测试 为什么片状测试是个问题 潜在的根本原因 Pytest特性 其他一般策略 研究 资源 Pytest导入机制和sys.path/PYTHONPATH 包中的测试模块及conftest.py文件 独立测试模块及conftest.py文件 调用通过python -m pytest调用pytest 返回: Pytest权威教程 片状测试 为什么片状测试是个问题 当使用连续集成(CI)服务器时,片状测试尤其麻烦,因此在合并新代码更改之前必须通过所有测试。如果测试结果不是一个可靠的信号 - 测试失败意味着代码更改破坏了测试 - 开发人员可能会对测试结果产生不信任,这可能导致忽略真正的失败。它也是浪费时间的一个来源,因为开发人员必须重新运行测试套件并调查虚假故障。 潜在的根本原因 系统状态 从广义上讲,一个片状测试表明测试依赖于一些未被适当控制的系统状态 - 测试环境没有充分隔离。更高级别的测试更有可能是因为他们依赖更多的状态。 当测试套件并行运行时(例如使用pytest-xdist),有时会出现片状测试。这可以表明测试依赖于测试排序。 也许不同的测试是在自身之后无法清理并留下导致片状测试失败的数据。 片状测试依赖于先前测试的数据,该测试不会自行清理,并且并行运行以前的测试并不总是存在 修改全局状态的测试通常不能并行运行。 过于严格的断言

Pytest权威教程24-示例和自定义技巧

江枫思渺然 提交于 2019-12-01 09:59:15
目录 示例和自定义技巧 返回: Pytest权威教程 示例和自定义技巧 这是一个(不断增长的)示例列表。如果你需要更多示例或有疑问,请联系我们。另请参阅包含许多示例代码段的 综合文档。此外,stackoverflow.com上的pytest专栏通常会有示例解答。 基本示例参考: 安装和入门: 基础入门示例 断言及断言语句: 基础断言示例 Pytest Fixture:显式,模块化,扩展: : 基本fixture/setup示例 参数化Fixture和测试用例:基本测试用例的参数化 unittest.TestCase支持: 基本unittest集成示例 运行Nose用例: 基本Nosetests集成示例 以下示例针对你可能遇到的各种用例。 Pytest失败用例报告示例 基本使用方式及示例 根据命令行选项将不同的值传递给测试函数 动态添加命令行选项 根据命令行选项控制跳过测试 编写完善的集成断言助手 检测是否在pytest运行中运行 添加信息以测试报告标题 分析测试持续时间 增量测试 - 测试步骤 包/目录级固定Fixture(设置) 后处理测试报告/失败 在Fixture方法中提供测试结果信息 PYTEST_CURRENT_TEST环境变量 冻结pytest 参数化测试 根据命令行生成参数组合 测试ID的不同选项 快速移植“testscenarios” 推迟参数化资源的设置

Pytest权威教程

大兔子大兔子 提交于 2019-12-01 09:57:12
[Pytest权威教程-01安装及入门]( https://www.cnblogs.com/superhin/p/11455335.html Pytest权威教程-02Pytest 使用及调用方法 Pytest权威教程-03原有TestSuite的执行方法 Pytest权威教程-04断言的编写和报告 Pytest权威教程-05Pytest fixtures:清晰 模块化 易扩展 Pytest权威教程-06使用Marks标记测试用例 来源: https://www.cnblogs.com/superhin/p/11677240.html

How to test Python classes that depend on argparse?

爱⌒轻易说出口 提交于 2019-12-01 07:07:53
The below paste contains relevant snippets from three separate Python files. The first is a script called from the command line which instantiates CIPuller given certain arguments. What happens is that the script gets called with something like: script.py ci (other args to be swallowed by argparse). The second is part of a subclass called Puller . The third is part of a subclass of Puller called CIPuller . This works wonderfully, as the correct subclass is called, and any user using the wrong other args gets to see the correct args for their given subclass, plus the generic arguments from the