草庐IT

pytest-mock

全部标签

python - 你如何模拟一个 python 类并为每个实例化获取一个新的 Mock 对象?

好的,我知道manual中提到了这一点,并且可能与side_effect和/或return_value有关,但一个简单直接的示例将极大地帮助我。我有:classClassToPatch():def__init__(self,*args):_do_some_init_stuff()defsome_func():_do_stuff()classUUT():def__init__(self,*args)resource_1=ClassToPatch()resource_2=ClassToPatch()现在,我想对UUT类进行单元测试,并模拟ClassToPatch。知道UUT类将恰好实例化两

python - 如何通过命令行在pytest中传递参数

我有一个代码,我需要从终端传递名称等参数。这是我的代码以及如何传递参数。我收到一个“找不到文件”的错误,我不明白。我已经在终端尝试了命令:pytest.py-almonds我应该把名字印成“杏仁”@pytest.mark.parametrize("name")defprint_name(name):print("Displayingname:%s"%name) 最佳答案 在你的pytest测试中,不要使用@pytest.mark.parametrize:deftest_print_name(name):print("Displayi

python - 如何将 pytest 与 virtualenv 一起使用?

我将pytest安装到虚拟环境中(使用virtualenv)并在该虚拟环境中运行它,但它没有使用我在该虚拟环境中安装的包环境。相反,它使用的是主系统包。(使用python-munittestdiscover,我实际上可以使用正确的python和包运行我的测试,但我想使用py.test框架。)是否有可能py.test实际上并没有在虚拟环境中运行pytest,我必须指定要运行哪个pytest?如何让py.test仅使用我的virtualenv中的python和包?另外,由于我的系统上有多个版本的Python,我如何判断Pytest使用的是哪个Python?它会在我的虚拟环境中自动使用Pyt

Python项目目录结构/pytest麻烦

这应该是地球上最简单的问题,但即使经过大量搜索和修补,我仍然很难找到一种“正确”的方式来放置目录结构并设法正确运行pytest等。假设我有一个名为apple的程序。|-README.md|-apple||--__init__.py||--apple.py|-tests||--test_everything.pyapple.py包含一些函数,例如,让我们调用一个eat()。test_everything.py文件包含一些测试,例如asserteat()=="foobar"。太好了,太容易了,但有趣的事情就开始了:苹果目录中的__init__.py怎么样...对吗?是空的还是里面应该有什么

python - pytest 参数化测试是否适用于基于 unittest 类的测试?

我一直在尝试将参数化的@pytest.mark.parametrize测试添加到基于类的单元测试中。classSomethingTests(unittest.TestCase):@pytest.mark.parametrize(('one','two'),[(1,2),(2,3)])deftest_default_values(self,one,two):assertone==(two+1)但是参数化的东西并没有发挥作用:TypeError:test_default_values()takesexactly3arguments(1given)我已经切换到简单的基于类的测试(没有单元测试

python - 如何使 pytest 固定装置与装饰功能一起使用?

当我装饰具有固定装置作为参数的测试函数时,py.test似乎失败了。defdeco(func):@functools.wraps(func)defwrapper(*args,**kwargs):returnfunc(*args,**kwargs)returnwrapper@pytest.fixturedefx():return0@decodeftest_something(x):assertx==0在这个简单的示例中,我收到以下错误:TypeError:test_something()takesexactly1argument(0given).有没有办法解决这个问题,最好不要过多地修r

Python mock Patch os.environ 和返回值

使用模拟单元测试conn():app.pyimportmysql.connectorimportos,urlparsedefconn():if"DATABASE_URL"inos.environ:url=urlparse(os.environ["DATABASE_URL"])g.db=mysql.connector.connect(user=url.username,password=url.password,host=url.hostname,database=url.path[1:],)else:return"Error"test.pydeftest_conn(self):with

python - 如何显示使用 pytest 时跳过某些测试的原因?

我正在使用unittest中的skipIf()在某些条件下跳过测试。@unittest.skipIf(condition),"thisiswhyIskippedthem!")如何告诉py.test显示跳过条件?我知道,对于unittest,我需要启用详细模式(-v),但添加到py.test的相同参数会增加详细程度,但仍不显示跳过原因。 最佳答案 当你运行py.test时,你可以通过-rsx来报告跳过的测试。来自py.test--help:-rcharsshowextratestsummaryinfoasspecifiedbychar

python - unittest.mock : asserting partial match for method argument

Rubyist在这里编写Python。我有一些看起来像这样的代码:result=database.Query('complicatedsqlwithanid:%s'%id)database.Query被模拟出来,我想测试ID是否正确注入(inject),而不会将整个SQL语句硬编码到我的测试中。在Ruby/RR中,我会这样做:mock(database).query(/#{id}/)但我看不到像在unittest.mock中那样设置“选择性模拟”的方法,至少没有一些毛茸茸的side_effect逻辑。所以我尝试在断言中使用正则表达式:withpatch(database)asMockD

python - "py.test"与 "pytest"命令

py.test命令在我的情况下失败,而pytest运行完全正常。我使用pytest-flask插件:platformlinux--Python3.5.2,pytest-3.0.2,py-1.4.31,pluggy-0.3.1rootdir:/home/sebastian/develop/py/flask-rest-template,inifile:plugins:flask-0.10.0当我调用$py.test时出现以下错误:Traceback(mostrecentcalllast):File"/usr/local/lib/python3.5/dist-packages/_pytest