简体   繁体   English

测试以假阳性通过

[英]Test passes with a False-Positive

I am getting false positive for my test.我的测试出现误报。 I am looking for the key, "1234", and I know it does not exist but the test still passes.我正在寻找密钥“1234”,我知道它不存在,但测试仍然通过。

I have MAP method (reportUIDsAndTitles) that stores a bunch of keys and values for UIDs and Titles.我有一个 MAP 方法(reportUIDsAndTitles),它存储一堆 UID 和标题的键和值。 My test loops through these keys and if it finds it asserts that the title matches the key.我的测试循环遍历这些键,如果发现它断言标题与键匹配。 But in the code below if it does not find the key the test still passes.但是在下面的代码中,如果没有找到密钥,测试仍然通过。

How do I fix this?我该如何解决? I have verified that my MAP method works and the loop works, but not sure how to set the failure.我已经验证我的 MAP 方法有效并且循环有效,但不确定如何设置失败。

@Test
public void apiTitleOfReport() {
    Map<String, String> pairs;
    pairs = reportUIDsAndTitles();

    for (Map.Entry pairEntry : pairs.entrySet()) {
        if (pairEntry.getKey().equals("1234")) {
            String keyValue = (String) pairEntry.getValue();
            assertEquals(keyValue, "Name of title I am looking for", "Report Title not found.");
        }}}

******* Here is my updated code with the missing Assert before the IF block as suggested. ******* 这是我更新后的代码,如建议的那样,在 IF 块之前缺少 Assert。 Now if the key does not exist it fails.现在,如果密钥不存在,则失败。 But if it does exist the code continues and checks that the title is correct.但如果它确实存在,代码会继续并检查标题是否正确。 Thank you all for you suggestions.谢谢大家的建议。

@Test
public void apiTitleOfReport() {
    Map<String, String> pairs;
    pairs = reportUIDsAndTitles();
    assertTrue(pairs.containsKey("1234"));
    

for (Map.Entry pairEntry : pairs.entrySet()) {
        if (pairEntry.getKey().equals("1234")) {
            String key = (String) pairEntry.getValue();
            assertEquals(key, "Name of title I am looking for", "Report Title not found.");
        }}}

You loop through every entry in your pairs map, then:您遍历pairs映射中的每个条目,然后:

  1. If the entry's key is "1234" , then check if the value (sidenote: You're calling the value keyValue ? You need better naming skills, mate :P) is equal to some specific thing.如果条目的键是"1234" ,则检查该值(旁注:您正在调用值keyValue ?您需要更好的命名技巧,伙伴:P)是否等于某个特定的东西。 If yes, great.如果是,那太好了。 If not, fail.如果没有,则失败。
  2. That's it - if the entry's key is not exactly "1234" , we keep on looping.就是这样 - 如果条目的键不完全是"1234" ,我们继续循环。 Only any entry whose key is "1324" causes any actual code to run.只有键为"1324"任何条目才会导致任何实际代码运行。
  3. That's it.就是这样。 There is no #3.没有#3。

Thus, as you said, there is no entry with key 1234, so your code does nothing.因此,正如您所说,没有键 1234 的条目,因此您的代码什么也不做。

How to fix it?如何解决? Your question is unclear as to intent.你的问题不清楚意图。 One way could be:一种方法可能是:

@Test
public void ensure1234IsNotInTheMap() {
    assertFalse(map.containsKey("1234"));
}

or perhaps:也许:

@Test
public void ensure1234IsMappedToFOOBAR() {
    assertEquals("FOOBAR", map.get("1234"));
    // if 1234 isn't in the map at all, the map.get op
    // returns null, and thus your test will properly fail,
    // as null is not equal to "FOOBAR".
}

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

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