我正在使用py.test来测试包含在python类MyTester中的一些DLL代码.为了验证目的,我需要在测试期间记录一些测试数据,然后再进行更多处理.由于我有很多测试_...文件,我想在大多数测试中重用测试器对象创建(MyTester实例).
由于测试对象是获得对DLL的变量和函数的引用的对象,我需要将DLL的变量列表传递给每个测试文件的测试对象(要记录的变量对于test_ .. .文件).列表的内容应用于记录指定的数据.
我的想法是以某种方式这样做:
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 for arg?
_tester = MyTester()
return _tester
# located in test_...py
# @pytest.mark.usefixtures("tester")
class TestIt():
# def __init__(self):
# self.args_for_tester = ["var1", "var2"]
# # how to …Run Code Online (Sandbox Code Playgroud) 我正在为我的测试套件使用pytest.虽然在复杂的组件间测试中捕获错误,但我想放在import ipdb; ipdb.set_trace()我的代码中间以允许我调试它.
但是,由于pytest陷阱sys.stdin/sys.stdout ipdb失败.如何在使用pytest进行测试时使用ipdb.
我没有兴趣在失败后跳转到pdb或ipdb,但是在代码中的任何地方放置中断并且能够在发生故障之前在那里调试它.
我正在使用py.test在CI服务器上运行单元测试.测试使用通过网络获取的外部资源.有时测试运行器需要太长时间,导致测试运行器中止.我无法在本地重复这些问题.
有没有办法让py.test打印出(慢)测试的执行时间,所以固定有问题的测试会变得更容易?
我已经开始研究一个相当大的(多线程)Python项目,其中包含大量(单元)测试.最重要的问题是运行应用程序需要预设环境,该环境由上下文管理器实现.到目前为止,我们使用了单元测试运行器的修补版本,它将在此管理器中运行测试,但这不允许在不同测试模块之间切换上下文.
鼻子和pytest都支持这样的东西,因为它们支持许多粒度的固定装置,所以我们正在考虑切换到鼻子或pytest.这两个库都支持"标记"测试并仅运行这些标记的子集,这也是我们也想做的事情.
我一直在仔细查看nose和pytest的文档,据我所知,这些库中较大的部分基本上支持相同的功能,除了它可能命名不同,或者需要稍微不同的语法.另外,我注意到可用插件的一些小差异(鼻子有多进程支持,例如pytest似乎没有)
所以看起来,魔鬼在细节上,这意味着(通常至少)个人品味,我们最好选择最适合我们个人品味的图书馆.
所以我要问一个主观的论证,为什么我应该选择鼻子或pytest来选择最适合我们需要的图书馆/社区组合.
我正在使用selenium进行端到端测试,我无法获得如何使用setup_class和teardown_class方法.
我需要在setup_class方法中设置浏览器,然后执行一系列定义为类方法的测试,最后在teardown_class方法中退出浏览器.
但从逻辑上讲,这似乎是一个糟糕的解决方案,因为实际上我的测试不是用于类,而是用于对象.我self在每个测试方法中传递param,所以我可以访问对象的变量:
class TestClass:
def setup_class(cls):
pass
def test_buttons(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def test_buttons2(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def teardown_class(cls):
pass
Run Code Online (Sandbox Code Playgroud)
甚至为类创建浏览器实例似乎也不正确..它应该分别为每个对象创建,对吧?
所以,我需要使用__init__和__del__方法而不是setup_class和teardown_class?
我正在试图找出如何python setup.py test运行等效的python -m unittest discover.我不想使用run_tests.py脚本,我不想使用任何外部测试工具(如nose或py.test).如果解决方案仅适用于python 2.7,那就没问题.
在setup.py,我想我需要在配置中的test_suite和/或test_loader字段中添加一些东西,但我似乎无法找到一个正常工作的组合:
config = {
'name': name,
'version': version,
'url': url,
'test_suite': '???',
'test_loader': '???',
}
Run Code Online (Sandbox Code Playgroud)
这可能只使用unittest内置于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
Run Code Online (Sandbox Code Playgroud)
更新:这是可能的,unittest2但我想找到相同的东西只使用unittest
来自https://pypi.python.org/pypi/unittest2
unittest2包括一个非常基本的setuptools兼容的测试收集器.在setup.py中指定test_suite ='unittest2.collector'.这将使用包含setup.py的目录中的默认参数启动测试发现,因此它可能是最有用的示例(请参阅unittest2/collector.py).
现在,我只是使用一个名为的脚本run_tests.py,但我希望通过转向仅使用的解决方案来摆脱这个问题python setup.py test.
这是run_tests.py我希望删除的:
import unittest
if __name__ == …Run Code Online (Sandbox Code Playgroud) 我正在使用Python编写一个包.我用virtualenv.我在virtualenv的.pth路径中设置了模块根目录的路径,这样我就可以在开发代码的同时导入包的模块并进行测试(问题1:这是一个好方法吗?).这工作正常(这是一个例子,这是我想要的行为):
(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from rc import ns
>>> exit()
(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ python tests/test_ns.py
issued command: echo hello
command output: hello
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用PyTest,我会收到一些导入错误消息:
(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ pytest
=========================================== test session starts ============================================
platform linux2 -- Python 2.7.12, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
rootdir: /home/zz/Desktop/GitFolders/rc, inifile:
collected 0 items / 1 errors
================================================== ERRORS ==================================================
________________________________ ERROR collecting tests/test_ns.py ________________________________
ImportError while importing …Run Code Online (Sandbox Code Playgroud) 我正在使用py.test运行一组测试.他们过去了.开心辞典!但我收到这条消息:
Exception KeyError: KeyError(4427427920,) in <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored
Run Code Online (Sandbox Code Playgroud)
我应该如何追查其来源?(我不是直接使用线程,而是使用gevent.)
我想在测试函数中放入一些日志语句来检查一些状态变量.
我有以下代码片段:
import pytest,os
import logging
logging.basicConfig(level=logging.DEBUG)
mylogger = logging.getLogger()
#############################################################################
def setup_module(module):
''' Setup for the entire module '''
mylogger.info('Inside Setup')
# Do the actual setup stuff here
pass
def setup_function(func):
''' Setup for test functions '''
if func == test_one:
mylogger.info(' Hurray !!')
def test_one():
''' Test One '''
mylogger.info('Inside Test 1')
#assert 0 == 1
pass
def test_two():
''' Test Two '''
mylogger.info('Inside Test 2')
pass
if __name__ == '__main__':
mylogger.info(' About to start the tests ') …Run Code Online (Sandbox Code Playgroud) 我试图使用norecursedirssetup.cfg中的选项告诉py.test不要从某些目录中收集测试,但它似乎确实忽略了它.
[tool:pytest]
norecursedirs=lib/third
Run Code Online (Sandbox Code Playgroud)
当我跑步时,py.test我确实看到它是如何从内部进行测试的lib/third!