Tag: py.test

py.test:将一个parameter passing给一个fixture函数

我正在使用py.test来testing包装在python类MyTester中的一些DLL代码。 为了validation目的,我需要在testing过程中logging一些testing数据,然后再做更多的处理。 因为我有很多testing_…文件,我想在testing中重用testing器对象的创build(MyTester的实例)。 由于testing者对象是获得对DLL的variables和函数的引用的对象,因此我需要将每个testing文件的DLLvariables列表传递给testing者对象(要logging的variables对于testing是相同的。 。文件)。 列表的内容应该用来logging指定的数据。 我的想法是这样做: import pytest class MyTester(): def __init__(self, arg = ["var0", "var1"]): self.arg = arg # self.use_arg_to_init_logging_part() def dothis(self): print "this" def dothat(self): print "that" # located in conftest.py (because other test will reuse it) @pytest.fixture() def tester(request): """ create tester object """ # how to use the list below […]

py.test – 当不同目录中的testing调用相同时,testing发现失败

使用py.test,在不同目录中调用相同的两个testing会导致py.test失败。 这是为什么? 我怎样才能改变这个不重命名所有的testing? 重复做: ; cd /var/tmp/my_test_module ; mkdir -p ook/test ; mkdir -p eek/test ; touch ook/test/test_proxy.py ; touch eek/test/test_proxy.py ; py.test ============================= test session starts ============================== platform linux2 — Python 2.7.3 — pytest-2.2.4 collected 0 items / 1 errors ==================================== ERRORS ==================================== ___________________ ERROR collecting ook/test/test_proxy.py ____________________ import file mismatch: imported module 'test_proxy' […]

pytest:断言几乎相等

如何执行assert almost equal与py.test相同的浮动,而不诉诸于像这样的东西: assert x – 0.00001 <= y <= x + 0.00001 更具体地说,知道一个简洁的解决scheme可以快速比较浮点数对, assert (1.32, 2.4) == i_return_tuple_of_two_floats()

如何从“python setup.py test”运行unittest发现?

我想弄清楚如何让python setup.py test运行相当于python -m unittest discover 。 我不想使用run_tests.py脚本,我不想使用任何外部testing工具(如nose或py.test )。 如果解决scheme只适用于python 2.7,那就没问题了。 在setup.py ,我想我需要添加一些东西到test_suite和/或test_loader字段在configuration中,但我似乎无法find一个工作正常的组合: config = { 'name': name, 'version': version, 'url': url, 'test_suite': '???', 'test_loader': '???', } 这可能只使用unit testing内置到Python 2.7? 仅供参考,我的项目结构如下所示: project/ package/ __init__.py module.py tests/ __init__.py test_module.py run_tests.py <- I want to delete this setup.py 更新 :这是可能的unittest2但我想find一些相当于使用unittest东西 从https://pypi.python.org/pypi/unittest2 unittest2包含一个非常基本的setuptools兼容testing收集器。 在setup.py中指定test_suite ='unittest2.collector'。 这将使用包含setup.py的目录中的默认参数开始testing发现,因此它可能是最有用的一个示例(请参阅unittest2 / collector.py)。 […]

如何打印到Py Test控制台?

我试图用pytest模块来使用testing驱动开发。 当我写print时pytest将不会print到控制台。 我使用py.test my_tests.py来运行它… 该documentation似乎说,它应该默认工作: http : //pytest.org/latest/capture.html 但: import myapplication as tum class TestBlogger: @classmethod def setup_class(self): self.user = "alice" self.b = tum.Blogger(self.user) print "This should be printed, but it won't be!" def test_inherit(self): assert issubclass(tum.Blogger, tum.Site) links = self.b.get_links(posts) print len(links) # This won't print either. 没有什么打印到我的标准输出控制台(只是正常的进展和多less次testing通过/失败)。 而我正在testing的脚本包含print: class Blogger(Site): get_links(self, posts): print […]

如何在运行pytesttesting时随意执行ipdb.set_trace()

我正在使用pytest作为我的testing套件。 在捕获复杂的组件间testing中的错误时,我想放置import ipdb; ipdb.set_trace() import ipdb; ipdb.set_trace()在我的代码中,让我debugging它。 但是,由于pytest陷阱sys.stdin / sys.stdout ipdb失败。 如何在使用pytest进行testing时使用ipdb。 我没有兴趣在故障发生后跳转到pdb或ipdb,而是在代码的任何地方放置中断,并且能够在发生故障之前在那里debugging它。

在py.test中,conftest.py文件有什么用处?

我最近发现了py.test 。 这似乎很棒。 不过,我觉得文件可能会更好。 我试图了解什么conftest.py文件是用来作为。 在我的(目前很小的)testing套件中,我在项目根目录下有一个conftest.py文件。 我用它来定义我注入到testing中的灯具。 我有两个问题: 这是conftest.py的正确使用吗? 它有其他用途吗? 我可以有多个conftest.py文件吗? 我什么时候想要这样做? 例子将不胜感激。 更一般地说,你如何定义pytesttesting套件中conftest.py文件的目的和正确使用?

鼻子与pytest – 什么(主观)差异应该让我select呢?

我已经开始研究一个相当大的(multithreading的)Python项目,并加载了(单元)testing。 最重要的问题是运行应用程序需要一个预设的环境,这是由上下文pipe理器实现的。 到目前为止,我们使用了可以在这个pipe理器中运行testing的unit testing运行器的补丁版本,但是这不允许在不同的testing模块之间切换上下文。 鼻子和pytest都支持这样的事情,因为它们支持多个粒度的固定装置,所以我们正在考虑切换到鼻子或pytest。 这两个库也将支持“标记”testing,并只运行这些标记的子集,这也是我们想要做的。 我一直在浏览nose和pytest的文档,据我所知,这些库的大部分基本上支持相同的function,除了可以命名不同,或者需要稍微不同的语法。 另外,我注意到可用插件中的一些小的差异(鼻子有多进程支持,pytest似乎不是) 所以看来,魔鬼在细节上,这意味着(通常至less)在个人的品味,我们最好去适合我们的个人品味最好的图书馆。 所以我会要求一个主观的论证,为什么我应该用鼻子或pytest去select最适合我们需要的图书馆/社区组合。

运行成功的py.test后,模块“线程”中的KeyError

我正在用py.test进行一系列testing。 他们通过。 开心辞典! 但是我收到这个消息: Exception KeyError: KeyError(4427427920,) in <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored 我应该如何追踪这个来源呢? (我不是直接使用线程,而是使用gevent。)

如何正确地断言在pytest中引发exception?

码: # coding=utf-8 import pytest def whatever(): return 9/0 def test_whatever(): try: whatever() except ZeroDivisionError as exc: pytest.fail(exc, pytrace=True) 输出: ================================ test session starts ================================= platform linux2 — Python 2.7.3 — py-1.4.20 — pytest-2.5.2 plugins: django, cov collected 1 items pytest_test.py F ====================================== FAILURES ====================================== ___________________________________ test_whatever ____________________________________ def test_whatever(): try: whatever() except ZeroDivisionError as […]