通过在 Eclipse 中突出显示方法名称,将测试方法添加到现有测试用例

发布于 2024-09-05 12:08:41 字数 205 浏览 7 评论 0原文

不确定这是否可行,但如果可行的话,它将节省我很多时间。当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2024-09-12 12:08:41

不。但老实说,无论如何您都不应该将测试与方法进行一对一的映射。我认为 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!

自在安然 2024-09-12 12:08:41

不可能。此外,一种方法并不意味着您只有一个测试用例。正如@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.

回首观望 2024-09-12 12:08:41

像往常一样创建新的 JUnit,但使用该类的另一个名称,例如 JavaClassTest2,然后复制粘贴生成的方法。

题外话:

我更喜欢“一对一”映射,除非我找到一种更好的方法,每个场景使用一种方法。我想避免这么多由测试方法名称标识的测试场景。我所做的是遵循以下模式:

  1. 根据 javadoc 和期望列出可能的测试场景作为测试错误消息。这作为场景索引,因此我可以轻松返回以研究我可能错过的场景。
  2. 按照步骤 1 的变量名称索引,逐段实施每个场景的测试。

    @Test public void testRemoveHtmlTags() {
    字符串[]测试= {
            "0. 空参数测试失败。",
            "1.区分大小写的测试失败。",
            “2.不区分大小写测试失败。” };
    
    
    尝试 {
        StringUtils.removeHtmlTags(null);
        失败(测试[0]);
    } catch (IllegalArgumentException iae) {}
    
    
    String input1 = "敏捷的棕色狐狸跳过了懒狗。><";
    字符串输出1 = StringUtils.removeHtmlTags(input1);
    assertEquals(test[1], "敏捷的棕色狐狸跳过懒狗。><", output1);
    
    
    String input2 = "敏捷的棕色狐狸跳过了懒狗。><";
    字符串输出2 = StringUtils.removeHtmlTags(input2);
    assertEquals(test[2], "敏捷的棕色狐狸跳过懒狗。><", output2);
    

    }

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:

  1. List possible test scenarios based on the javadoc and expectations as test error message. This serves as scenario index so I can go back easily to study which scenario I may have missed.
  2. Implement the test for each scenario by paragraph following the indexing of step1 for variable names.

    @Test public void testRemoveHtmlTags() {
    String[] test = {
            "0. Null argument test failed.",
            "1. Case sensitive test failed.",
            "2. Case insensitive test failed." };
    
    
    try {
        StringUtils.removeHtmlTags(null);
        fail(test[0]);
    } catch (IllegalArgumentException iae) {}
    
    
    String input1 = "The quick Brown <b>Fox</b> Jumps Over The Lazy Dog. ><";
    String output1 = StringUtils.removeHtmlTags(input1);
    assertEquals(test[1], "The quick Brown Fox Jumps Over The Lazy Dog. ><", output1);
    
    
    String input2 = "The quick Brown <B>Fox</b> Jumps Over The Lazy Dog. ><";
    String output2 = StringUtils.removeHtmlTags(input2);
    assertEquals(test[2], "The quick Brown Fox Jumps Over The Lazy Dog. ><", output2);
    

    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文