简体   繁体   English

重载unittest.testcase的__init__

[英]overloading __init__ of unittest.testcase

I want to add two variables to my subclass which is inherited from unittest.testcase 我想在我的子类中添加两个变量,这些变量继承自unittest.testcase

like I have: 像我一样:

import unittest

class mrp_repair_test_case(unittest.TestCase):

     def __init__(self, a=None, b=None, methodName=['runTest']):
             unittest.TestCase.__init__(self)
             self.a= a
              self.b = b

     def test1(self):
           ..........
           .......

def runtest()
    mrp_repair_test_case(a=10,b=20)
    suite = unittest.TestLoader().loadTestsFromTestCase(mrp_repair_test_case)
    res = unittest.TextTestRunner(stream=out,verbosity=2).run(suite)

how can I acvhieve this: I am getting this error: 我怎么能得到这个:我收到这个错误:

ValueError: no such test method in ****<class 'mrp_repair.unit_test.test.mrp_repair_test_case'>:**** runTest

thanks 谢谢

At first glance, it looks like you need to create an instance of mrp_repair_test_case . 乍一看,您似乎需要创建一个mrp_repair_test_case实例。 Your current line: 您当前的行:

mrp_repair_test_case(a=10,b=20)

doesn't actually do anything. 实际上并没有做任何事情。

Try (not tested): 尝试(未测试):

def runtest():
    m = mrp_repair_test_case(a=10, b=20)
    suite = unittest.TestLoader().loadsTestsFromTestCase(m)
    res = unittest.TextTestRunner(stream=out, verbosity=2).run(suite)

This assumes you've set up 'out' as a stream already. 这假设您已经将'out'设置为流。

Edit: 编辑:

By the way, is there any reason you're not using a setUp method to set these values? 那么,有没有理由你没有使用setUp方法来设置这些值? That would be normal best practice. 这将是正常的最佳做法。 Looking at the documentation of loadTestsFromTestCase it looks like it will only accept the Class itself not an instance of it, which would mean you're rather working against the design of the unittest module. 看一下loadTestsFromTestCase的文档,看起来它只接受Class本身而不是它的一个实例,这意味着你正在反对unittest模块的设计。

Edit 2: 编辑2:

In response to your further information, I would actually set your uid and cursor values seperately at module level before calling the tests. 为了回应您的进一步信息,我实际上会在调用测试之前将您的uid和游标值分别设置在模块级别。 I'm not a huge fan of globals normally, but if I'm understanding you correctly these values will be A) read-only B) always the same for the same customer which avoids most of the normal pitfalls in using them. 我通常不是全球人的忠实粉丝,但如果我正确地理解你,那么这些价值将是A)只读B)对于同一个客户总是相同的,这避免了使用它们时的大多数正常陷阱。

Edit 3: 编辑3:

To answer your edit, if you really want to use __init__ you probably can, but you will have to roll your own loadsTestsFromTestCase alternative, and possibly your own TestSuite (you'll have to check the internals of how it works). 要回答你的编辑,如果你真的想使用__init__你可能会,但你必须推出自己的loadsTestsFromTestCase替代品,可能还有你自己的TestSuite(你必须检查它的工作原理)。 As I said above, you'll be working against the existing design of the module - to the extent that if you decide to do your testing that way it might be easier to roll your own solution completely from scratch than use unittest. 正如我上面所说,你将会反对模块的现有设计 - 如果你决定以这种方式进行测试,那么从头开始推出自己的解决方案可能比使用unittest更容易。 Amend : just checked, you'd definately have to roll your own version of TestSuite, as the existing one creates a new instance of the TestCaseClass for each test. 修改 :刚刚检查过,你肯定要推出自己的TestSuite版本,因为现有版本为每个测试创建一个TestCaseClass的新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM