通过在 Eclipse 中突出显示方法名称,将测试方法添加到现有测试用例
不确定这是否可行,但如果可行的话,它将节省我很多时间。当我在 Eclise 中创建 Junit4 测试用例时,我通常不会先包含我想要测试的所有方法,然后再将未测试的方法或新方法添加到测试用例中。目前,我通过在现有测试用例类中键入新的测试方法来完成此操作。有没有办法让我突出显示方法名称,并创建一个测试用例(如果不存在)或添加到 Eclipse 中的现有测试用例? 提前致谢!
大卫
Not sure if this is possible, but it'll save me a lot of time if it is. When I create a Junit4 test case in Eclise, I don't usually included all the methods that I'd like to test first, and later on, I'd like to add the untested methods or new methods to the test case. Currently, I'm doing this by typing new test methods in the existing test case class. Is there a way for me to highlight the method name, and create a test case if not existed or add to an existing test case in Eclipse?
Thanks in advance!
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。但老实说,无论如何您都不应该将测试与方法进行一对一的映射。我认为 Eclipse“创建 JUnit TestCase”对话框的第二页设计得非常糟糕,因为它给人的印象是一对一映射在某种程度上是有用的。
您应该进行与完全测试每个方法所需的测试一样多的测试,并且每个测试应该只测试该方法行为的一个方面。因此,如果您需要两次、三次或四次测试,那也没关系。
这让我断言您寻求的功能不会很有用,因为无论如何您都需要添加更多的测试方法!
No. But honestly, you shouldn't be having a one-to-one mapping of tests to methods anyway. I think the second page of the Eclipse "Create JUnit TestCase" dialogue is very badly designed because it kind of gives the impression that a one-to-one mapping is somehow useful.
You should have exactly as many tests as are required to fully test each method, and each test should only test one aspect of the method's behaviour. So if you need two or three or four tests, that's fine.
Which leads me to assert that the functionality you seek would not be hugely useful, because you will need to be adding lots more test methods anyway!
不可能。此外,一种方法并不意味着您只有一个测试用例。正如@Danny 所说,您将需要提出几个测试用例来测试该方法的所有可能的有用场景。如果您将所有这些测试场景组合成一个大测试用例,那么它不会太有用。这是因为当该测试用例失败时,您必须深入挖掘测试用例才能知道哪个场景失败了,这很麻烦。
为了测试一种方法,通常(如果不是总是)拥有许多小测试用例比一个大测试用例更好。当它失败时,您可以立即准确地知道哪个测试场景失败了。
Not possible. Further, one method doesn't mean you have exactly one testcase for it. As @Danny has said, you will need to come up with several testcases to test every possible useful scenarios for that one method. If you combine all these test scenarios into one big testcase, then it is not going to be too useful. This is because when that testcase fails, you will have to dig deep into the testcase just to know which scenario fails, which is cumbersome.
To test one method, it is usually (if not, always) better to have many small testcases rather than one big testcase. When it fails, you know exactly which test scenario fails right away.
像往常一样创建新的 JUnit,但使用该类的另一个名称,例如 JavaClassTest2,然后复制粘贴生成的方法。
题外话:
我更喜欢“一对一”映射,除非我找到一种更好的方法,每个场景使用一种方法。我想避免这么多由测试方法名称标识的测试场景。我所做的是遵循以下模式:
按照步骤 1 的变量名称索引,逐段实施每个场景的测试。
}
Create New JUnit as usual but use another name for the class say JavaClassTest2 then copy paste the generated method.
Off Topic:
I prefer "one is to one" mapping, unless I find a better approach with using one method per scenario. I want to avoid so many test scenarios identified by test method names. What I do is follow this pattern:
Implement the test for each scenario by paragraph following the indexing of step1 for variable names.
}