草庐IT

pytest-mock

全部标签

python - 在 pytest 的不同测试中仅使用某些 fixture 参数化

我有一个名为n_groups的fixture,我想在某些情况下对其进行参数化,但在其他情况下则不会。这样做的原因是因为我的类似MVC的数据模型的结构方式,我在“模型”类中尽可能多地测试,但“Controller”类不需要那么广泛的测试,因为我已经在“模型”中完成。因此,在Controller中运行具有所有参数化的测试是多余的,我想限制测试的数量,从而限制测试时间。目前为了测试我的Controller的初始化,生成了超过18,000个测试,运行需要42分钟!查看Travis-CIoutput.目前,我的解决方法是,#Contentsofconftest.pyimportpytestimp

python - 如何在 pytest 中两次使用参数化的依赖 fixture ?

我试图在单个测试中多次使用参数化fixture,目的是获取其所有值的笛卡尔积。https://stackoverflow.com/a/39444098/102441展示了如何对一个简单的fixture执行此操作:importpytest@pytest.fixture(params=[0,1,2])deffirst(request):returnrequest.paramsecond=first#runs3x3=9timesdeftest_double_fixture(first,second):assertFalse,'{}{}'.format(first,second)但是,如果参数

python - ScopeMismatch 将 session 范围的 fixture 与 pytest-mozwebqa 插件一起用于 py.test

我正在使用pytest-mozwebqa插件来运行一些selenium测试。我只想为所有测试登录一次应用程序,所以我尝试在conftest.py中使用session范围的fixture,但出现以下错误。我如何编写此登录装置,以便每个测试都不需要登录,并且所有测试都可以使用一次登录?这是我遇到的错误:=================================================================================================ERRORS===========================================

python - mock.call_count 的线程安全版本

看起来Mock.call_count不能与线程一起正常工作。例如:importthreadingimporttimefrommockimportMagicMockdeff():time.sleep(0.1)deftest_1():mock=MagicMock(side_effect=f)nb_threads=100000threads=[]for_inrange(nb_threads):thread=threading.Thread(target=mock)threads.append(thread)thread.start()forthreadinthreads:thread.joi

python - python3中pytest的布局和导入

我在从我的pytest函数导入模块时遇到问题。我知道这方面有一百万个问题,但我已经阅读了很多,但我仍然无法理解。$tree.└──code├──eight_puzzle.py├──missionaries_and_cannibals.py├──node.py├──search.py└──test├──test_eight_puzzle.py└──test_search.py2directories,6files$$grepimportcode/test/test_search.pyimportsysimportpytestimportcode.search$$pytest...Impo

python - 属性错误 : __exit__ when I try to mock out build in functions

我目前正在尝试在Python中模拟open()内置方法进行测试。但是,我总是以崩溃告终,并得到以下消息:File"/opt/home/venv/lib/python2.7/site-packages/nose-1.3.0-py2.7.egg/nose/result.py",line187,in_exc_info_to_stringreturn_TextTestResult._exc_info_to_string(self,err,test)File"/opt/python-2.7.3/lib/python2.7/unittest/result.py",line164,in_exc_in

python - 检查 unittest.mock 调用参数不可知 w.r.t.它们是作为位置参数还是关键字参数传递

当unittest.mock.Mock对象被调用时,我可以使用调用的确切签名检查参数值:fromunittest.mockimportMockm=Mock()#creationofmockm('foo',bar='baz')#calltothemockm.assert_called_once_with('foo',bar='baz')#checkcallarguments检查具有相同值的不同签名将失败。例如,如果我们检查'baz'作为位置参数而不是命名参数,断言将失败:m.assert_called_once_with('foo','baz')#AssertionError:Expec

python - 测试实例中的方法是否已在 mock 中调用

我有这种设置,我正在测试一个正在使用另一个类的类,我想模拟后者,所以我只测试第一个类本身。nuclear_reactor.py:classNuclearReactor():def__init__(self):print"initializingthenuclearreactor"defstart(self):print"startingthenuclearreactor"nuclear_manager.py:fromnuclear_reactorimportNuclearReactorclassNuclearManager():def__init__(self):print"manag

python - moto 没有 mock ec2?

我正在尝试测试一些使用boto的python代码。我不想尝试针对AWS进行集成测试,所以我尝试用moto模拟它,但它的行为并不像我预期的那样。测试代码如下:importioimportboto3frommotoimportmock_ec2fromunittest.mockimportpatchfromargparseimportNamespacefromawswlimportcommands@mock_ec2@patch('awswl.externalip.get_external_ip',return_value='192.0.2.1')deftest_list_command_li

python - pytest:在线程中运行

一个名为pytest_demo.py的python文件:importpytestimportthreading@pytest.mark.testclassTestDemo():deftest_demo_false(self):assertFalsedeftest_demo_true(self):assertTruedeftest_demo_thread_true(self):thread1=MyThread(True)thread1.start()deftest_demo_thread_false(self):thread1=MyThread(False)thread1.start()