ValueError: 在 <class 'myapp.tests.SessionTestCase'> 中没有这样的测试方法:runTest [英] ValueError: no such test method in <class 'myapp.tests.SessionTestCase'>: runTest
问题描述
我有一个测试用例:
class LoginTestCase(unittest.TestCase):
...
我想在不同的测试用例中使用它:
I'd like to use it in a different test case:
class EditProfileTestCase(unittest.TestCase):
def __init__(self):
self.t = LoginTestCase()
self.t.login()
这引发了:
ValueError: no such test method in <class 'LoginTest: runTest`
我查看了调用异常的单元测试代码,看起来测试不应该这样编写.是否有一种标准方法可以编写您想要测试的内容,以便以后的测试可以重用?或者有解决方法吗?
I looked at the unittest code where the exception is being called, and it looks like the tests aren't supposed to be written this way. Is there a standard way to write something you'd like tested so that it can be reused by later tests? Or is there a workaround?
我已经向 LoginTest
添加了一个空的 runTest
方法,作为目前一个可疑的解决方法.
I've added an empty runTest
method to LoginTest
as a dubious workaround for now.
推荐答案
unittest
有很深的魔法——如果你选择用它来运行你的单元测试(我这样做,因为这样我可以在我的工作场所使用非常强大的测试运行器和集成到构建系统中的电池组,但绝对有值得的替代方案),你最好遵守它的规则.
unittest
does deep black magic -- if you choose to use it to run your unit-tests (I do, since this way I can use a very powerful battery of test runners &c integrated into the build system at my workplace, but there are definitely worthwhile alternatives), you'd better play by its rules.
在这种情况下,我只是让 EditProfileTestCase
派生自 LoginTestCase
(而不是直接来自 unittest.TestCase
).如果 LoginTestCase
的某些部分您确实希望在 EditProfileTestCase
的不同环境中也进行测试,而其他部分您不想,那么重构 LoginTestCase
到这两个部分(可能使用多重继承),如果在这两种情况下有些事情需要发生略有不同,请将它们分解为辅助的挂钩方法"(在模板方法"设计模式中)-- 我经常使用所有这些方法来减少样板文件并增加我经常编写的大量单元测试中的重用(如果我的单元测试覆盖率 <95%,我总是感到非常不安——低于 90%,我开始感到身体不适;-)
In this case, I'd simply have EditProfileTestCase
derive from LoginTestCase
(rather than directly from unittest.TestCase
). If there are some parts of LoginTestCase
that you do want to also test in the different environment of EditProfileTestCase
, and others that you don't, it's a simple matter to refactor LoginTestCase
into those two parts (possibly using multiple inheritance) and if some things need to happen slightly differently in the two cases, factor them out into auxiliary "hook methods" (in a "Template Method" design pattern) -- I use all of these approaches often to diminish boilerplate and increase reuse in the copious unit tests I always write (if I have unit-test coverage < 95%, I always feel truly uneasy -- below 90%, I start to feel physically sick;-).
这篇关于ValueError: 在 <class 'myapp.tests.SessionTestCase'> 中没有这样的测试方法:runTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!