草庐IT

pytest-mock

全部标签

python - 类型错误 : unbound method when trying to mock a classmethod

此脚本失败:importmockclassMyClass(object):@classmethoddefmy_method(cls):print('my_method')defmocked_method(cls):print('Iwantthismethodtogetcalled')withmock.patch.object(MyClass,'my_method',mocked_method):MyClass.my_method()异常(exception):Traceback(mostrecentcalllast):File"/home/foo/tmp/test_mocking_cl

python - Mock_open CSV 文件未获取任何数据

我正在尝试对一段代码进行单元测试:def_parse_results(self,file_name):results_file=open(file_name)results_data=list(csv.reader(results_file))index=len(results_data[1])-1results_file.close()returnfloat(results_data[1][index])像这样使用mock_open:@mock.patch('path.open',mock.mock_open(read_data='test,test2,test3,test4'))d

python - 如何在pytest中集成对readme的检查

我在.travis.yml中使用pytest检查我的代码。我也想查看README.rst。我找到了readme_renderer通过这个StackOanswer现在我问自己如何将其集成到我当前的测试中。readme_renderer的文档建议这样做,但我不知道如何将其集成到我的设置中:pythonsetup.pycheck-r-s 最佳答案 我认为最简单和最可靠的选择是编写一个pytest插件来复制distutilscommand的内容。您在回答中提到确实如此。这可以像conftest.py一样简单在你的测试目录中。或者,如果您想要

python - 如何在测试注释中使用带有 patch.object() 的 mock_open()

我正在尝试模拟从文件中读取。使用示例,可​​以使用如下构造来完成:withpatch('__builtin__.open',mock_open(read_data='1'),create=True)asm:withopen('foo')ash:result=h.read()我想知道,有没有一种方法可以使用我的测试用例注释来模拟打开函数。喜欢:@patch.object(__builtin__,'open')deftest_check_status_running(self,m_open):我没有找到正确的方法,因为对我来说它适用于int而不适用于字符串:@patch.object(__

python - Pytest - 没有测试运行

我正在使用pytest和selenium。当我尝试运行我的测试脚本时:importpytestfromseleniumimportwebdriverfrompagesimport*fromlocatorsimport*fromselenium.webdriver.common.byimportByimporttimeclassRegisterNewInstructor:defsetup_class(cls):cls.driver=webdriver.Firefox()cls.driver.get("http://mytest.com")deftest_01_clickBecomeTop

python - 如何使用 mock.patch 模拟生成器

我已经浏览了页面https://docs.python.org/3/library/unittest.mock-examples.html我看到他们列出了一个关于如何模拟生成器的例子我有一个代码,我调用生成器给我一组值,我将这些值保存为字典。我想在我的单元测试中模拟对该生成器的调用。我已经写了下面的代码,但它不起作用。我哪里错了?In[7]:items=[(1,'a'),(2,'a'),(3,'a')]In[18]:deff():print"here"foriin[1,2,3]:yieldi,'a'In[8]:defcall_f():...:my_dict=dict(f())...:p

python - pytest fixtures 的范围可以被覆盖吗?

pytest内置fixture的作用域可以改变吗?这行不通:@pytest.fixture(scope="class")defmyFixture(tmpdir):pass...因为tmpdir是函数范围的:ScopeMismatch:Youtriedtoaccessthe'function'scopedfixture'tmpdir'witha'class'scopedrequestobject,involvedfactories是否可以覆盖pytest的内置fixture的范围,以便我可以获得类范围的tmpdirfixture? 最佳答案

python - 我可以让 pytest doctest 模块忽略一个文件吗?

我们使用pytest来测试我们的项目,并默认启用--doctest-modules以收集整个项目中的所有doctests。但是有一个wsgi.py可能不会在测试收集期间导入,但我无法让pytest忽略它。我试着把它放在conftest.py的collect_ignore列表中,但显然doctest模块不使用这个列表。唯一可行的是将wsgi.py的整个目录放入pytest配置文件的norecursedirs中,但这显然隐藏了整个目录,我不想。有没有办法让doctest模块只忽略某个文件? 最佳答案 作为MasterAndrey已经提到

python - Pytest:设置测试客户端和数据库

我想学习一些关于测试我的Flask应用程序的知识。为此,我使用了pytest和sqlalchemy。我想测试一个模板,它传递路由一些SQL内容。所以在我看来,我需要一个testClient来测试路由本身,以及一个DBfixture来管理路由中包含的DB内容。这是我的固定装置:importpytestfromconfigimportTestingConfigfromapplicationimportcreate_app,db###############################functionaltests############################@pytest.f

python - 使 Mock.assert_called_with() 与 args vs kwargs 不可知

单元测试应该测试功能并尽量不了解实现细节。Mock.assert_called_with()是一个方便的函数,但据我所知它将*args与*args和**kwargs到**kwargs。因此:#classtobemockedduringtestclassSomeClass():deffunc(self,a,b,c=5):#...#codeundertestsomaclass_instance.func(1,b=2,c=3)#testcodethatworkssomeclass_mock.func.assert_called_with(1,b=2,c=3)#testcodethatwon'