简体   繁体   English

理解这些 JUnit 测试结果

[英]Comprehending these JUnit tests results

I am doing some exercises for my CS course and they are giving us Junit tests however they only tell us if we fail or pass.我正在为我的 CS 课程做一些练习,他们给我们提供 Junit 测试,但是他们只告诉我们我们是失败还是通过。 The output/expected output is jibberish to me.输出/预期输出对我来说是胡言乱语。

I am given expected output/output in this fashion:我以这种方式获得预期的输出/输出:

java.lang.AssertionError: expected <3143794514> but was <459133821> 

I notice that the value <459133821L> is also found in the code of the test.我注意到在测试代码中也发现了 <459133821L> 值。 However, I'm still a beginner.但是,我仍然是初学者。 Apparently adler32 is meant to check for errors through checksums, but I don't know how to utilize this.显然 adler32 旨在通过校验和检查错误,但我不知道如何利用它。 Is there some way to have this show more meaningful messages so I know what is going wrong with my code?有没有办法让这个显示更有意义的消息,这样我就知道我的代码出了什么问题?

Eg: I am expected to count all the words in a string.例如:我应该计算字符串中的所有单词。 Can these tests show me what input/output is returning the incorrect answer?这些测试能告诉我什么输入/输出返回错误答案吗?

Here is a sample of the JUnit class:这是 JUnit 类的示例:

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.io.*;
import java.util.zip.Adler32;

public class TestStringProblems {

    private static final int RUNS = 100000;
    private static final int SEED = 12345;
    private StringProblems sp = new StringProblems();

    @Test
    public void testCountWords() {
        BufferedReader br = null;
        Adler32 check = new Adler32();
        int count = 0;
        try {
            br = new BufferedReader(new FileReader("warandpeace.txt"));
            String line = br.readLine();
            while(line != null) {
                int words = sp.countWords(line.trim());
                count += words;
                check.update(words);
                line = br.readLine(); 
            }
        } catch(IOException e) { System.out.println("Error: " + e); assertTrue(false); }
        finally { try { br.close(); } catch(Exception e) { } }
        assertEquals(count, 562491); // number of words in War and Peace
        assertEquals(check.getValue(), 2309395892L); // checksum of word counts
    }

    @Test
    public void testRemoveDuplicates() {
        Adler32 check = new Adler32();
        java.util.Random rng = new java.util.Random(SEED);
        for(int i = 0; i < RUNS; i++) {
            StringBuilder sb = new StringBuilder();
            int len = rng.nextInt(500);
            for(int j = 0; j < len; j++) {
                char c = (char)(1 + rng.nextInt(50000));
                int rep = rng.nextInt(10) + 1;
                for(int k = 0; k < rep; k++) {
                    sb.append(c);
                }
            }
            check.update(sp.removeDuplicates(sb.toString()).getBytes());
        }
        assertEquals(check.getValue(), 459133821L);
    }


}

Thanks.谢谢。

public class StringProblems {

    public String removeDuplicates(String s) {
        String newStr = "";
        if (s.length() == 0) {
            return s;
        }

        int length = s.length() - 1;
        for(int i = 0;i<length+1;i++) {
            if(i!=0 && s.charAt(i)!=s.charAt(i-1)) {
                newStr += s.charAt(i);
            }
        }


        return s.charAt(0) + newStr;
    }

    public int countWords(String s) {
        String newStr = s.trim(); // removes unnecessary whitespace

        if (newStr.isEmpty()) {
            return 0;
        }

        return newStr.split("\\W+").length; // should work since it creates an array of substrings, 
                                            // length should indicate how many substrings are in the new string
    }



}

You're "expected" and "actuals" are backward.你是“预期的”,而“实际的”是落后的。

http://junit.sourceforge.net/javadoc/org/junit/Assert.html http://junit.sourceforge.net/javadoc/org/junit/Assert.html

The first parameter to assertEquals is EXPECTED, the second is ACTUAL. assertEquals 的第一个参数是 EXPECTED,第二个是 ACTUAL。

The error you are seeing is obviously firing on this line: assertEquals(check.getValue(), 459133821L);您看到的错误显然是在这一行上触发的: assertEquals(check.getValue(), 459133821L);

You need to swap your expected and actuals and THEN ALSO fix your calculations.您需要交换您的预期和实际值,然后还要修正您的计算。 You're still getting the wrong answer if you are trying to get 459133821L.如果您尝试获得 459133821L,您仍然会得到错误的答案。 I haven't looked over all of your code, but these tests are showing you the input output and what is giving you the correct answer.我没有查看您的所有代码,但这些测试向您展示了输入输出以及给出正确答案的内容。 Figure out why you are trying to hit 459133821L in testRemoveDuplicates (which at a glance seems to be using a random so I'm not sure how you know what to expect), and you'll solve it.搞清楚你为什么试图击中459133821L在testRemoveDuplicates (这一眼似乎使用随机,所以我不知道你怎么知道会发生什么),你会解决它。

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

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