我试图通过对unittest.testcase类进行子类化来创建自定义单元测试框架,但在处理__init__时似乎出错了方法。我不明白为什么ComplexTest的构造函数在BasicTest中的那个之前不会被调用而且异常似乎也与我的构造函数有关。我是Python的新手,因此非常欢迎任何有关如何解决这个特定问题或我的用例的替代架构的帮助。谢谢!1)test_framework.pyimportunittestclassBasicTest(unittest.TestCase):def__init__(self,*args,**kwargs):print('BasicTest.__init
总的来说,我不熟悉python重写方法和使用super()的方式。问题是:我可以覆盖get_FOO_display()吗?classA(models.Model):unit=models.IntegerField(choices=something)defget_unit_display(self,value):...usesuper(A,self).get_unit_display()我想覆盖get_FOO_display()因为我想使我的显示复数化。但是super(A,self).get_unit_display()不起作用。 最佳答案
如何使用@patch装饰器来修补内置的input()函数?例如,我想测试question.py中的一个函数,它包含对input()的调用:defquery_yes_no(question,default="yes"):"""Adaptedfromhttp://stackoverflow.com/questions/3041986/python-command-line-yes-no-input"""valid={"yes":True,"y":True,"ye":True,"no":False,"n":False}ifdefaultisNone:prompt="[y/n]"elifdef
我一定错过了这里显而易见的东西。我正在使用django1.5.x并基于djangosTestCase类创建单元测试。当我从很多不同的源中提取(只读)时,我在设置中定义了一堆数据库。运行测试时,我只想创建默认数据库的测试版本,其余的我想标记为只读,而不是尝试重新创建为test_db_name(用户定义的不会(不能)具有创建这些的权限dbs无论如何)。当然这是可能的-正如我所说,我一定错过了明显的东西?感谢任何帮助。马修 最佳答案 不明显,不。Sort-ofdocumented,您可以设置测试时使用的数据库名称:设置.pyDATABAS
我有一个脚本可以做各种事情并使用sys.argv访问参数,但是当脚本到达代码的单元测试部分时,它说没有用于此的模块。我的脚本是:classMyScript():def__init__(self):self.value=sys.argv[1]defhello(self):printself.valuedefsuite(self):modules_to_test=('external_sanity_onvif','starttest')alltests=unittest.TestSuite()formoduleinmap(__import__,modules_to_test):alltes
我的属性之一是setter调用验证函数的属性,如果新值无效,该验证函数会引发异常:pos.offset=0#@offset.settercallsvalidate(offset=0)#PositionError:Offsetmaynotbe0.我正在尝试添加一个测试以确保它失败。但是,我无法弄清楚如何让assertRaises处理作业。assertRaises的正常语法需要一个方法,而不是属性/属性:self.assertRaises(PositionError,pos.offset,0)#TypeError:'int'objectisnotcallable我试过的其他形式都是无效的P
我有以下测试代码检查函数中是否引发异常。我希望测试通过,但显示失败。这是测试代码:importunittest#defineauser-definedexceptionclassMyException(Exception):def__str__(self):returnrepr("ERROR:Justraisedmyexception!")#thisismymainclasswithamethodraisingthisexceptionclassMyMainObject(object):deffunc(self):raiseMyException()#thetestclassclass
在django1.2.5之前,我可以使用以下代码创建一个用户进行测试,然后登录:classTestSomeLoginRequiredView(TestCase):urls='sonloop.tests.test_urls'defsetUp(self):self.user=User.objects.create(username='testuser',password='some_password')deftest_the_view(self):response=self.client.get('/test_view_url/')self.assertEqual(response.sta
当我正常运行应用程序并在浏览器中登录时,它可以正常工作。但是使用Unittest它不会让我登录....,它会再次返回登录页面。“printrv.data”都只是打印登录页面的内容,但它应该打印索引页面的内容,即login_required如果有帮助,我正在使用SQLAlchemy作为ORM。任何人都知道什么可能是问题?如果需要更多代码,我会提供。我试图在这里搜索类似的问题,但没有找到。#!flask/bin/pythonimportosimportunittestfromconfigimportbasedirfromappimportapp,dbfromapp.modelsimport
有了unittest模块,我喜欢featuretoskiptests,但它仅适用于Python2.7+。例如,考虑test.py:importunittesttry:importproprietary_moduleexceptImportError:proprietary_module=NoneclassTestProprietary(unittest.TestCase):@unittest.skipIf(proprietary_moduleisNone,"requriesproprietarymodule")deftest_something_proprietary(self):se