简体   繁体   English

编写java testng测试用例

[英]writing java testng test cases

I am beginning with Java and testng test cases. 我从Java和testng测试用例开始。

I need to write a class, which reads data from a file and makes an in-memory data structure and uses this data structure for further processing. 我需要编写一个类,它从文件中读取数据并生成内存中的数据结构,并使用此数据结构进行进一步处理。 I would like to test, if this DS is being populated correctly. 如果正确填充此DS,我想测试一下。 This would call for dumping the DS into a file and then comparing the input file with the dumped file. 这将调用将DS转储到文件中,然后将输入文件与转储文件进行比较。 Is there any testNG assert available for file matching? 是否有任何testNG断言可用于文件匹配? Is this a common practice? 这是一种常见做法吗?

I think it would be better to compare the data itself not the written out data. 我认为比较数据本身而不是写出的数据会更好。

So I would write a method in the class to return this data structure (let's call it getDataStructure() ) and then write a unit test to compare with the correct data. 所以我会在类中编写一个方法来返回这个数据结构(让我们称之为getDataStructure() ),然后编写一个单元测试来与正确的数据进行比较。

This only needs a correct equals() method in your data structure class and do: 这只需要在数据结构类中使用正确的equals()方法并执行:

Assert.assertEquals(yourClass.getDataStructure(), correctData);

Of course if you need to write out the data structure to a file, then you can test the serialization and deserialization separately. 当然,如果您需要将数据结构写出来,那么您可以单独测试序列化和反序列化。

File compare/matching can be extracted to a utility method or something like that. 文件比较/匹配可以提取到实用方法或类似的东西。 If you need it only for testing there are addons for jUnit http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html 如果你需要它只用于测试有jUnit的插件http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html

If you need file compare outside the testing environment you can use this simple function 如果您需要在测试环境之外进行文件比较,则可以使用此简单功能

    public static boolean fileContentEquals(String filePathA, String filePathB) throws Exception {
    if (!compareFilesLength(filePathA, filePathB)) return false;

    BufferedInputStream streamA = null;
    BufferedInputStream streamB = null;
    try {
        File fileA = new File(filePathA);
        File fileB = new File(filePathB);

        streamA = new BufferedInputStream(new FileInputStream(fileA));
        streamB = new BufferedInputStream(new FileInputStream(fileB));

        int chunkSizeInBytes = 16384;
        byte[] bufferA = new byte[chunkSizeInBytes];
        byte[] bufferB = new byte[chunkSizeInBytes];

        int totalReadBytes = 0;
        while (totalReadBytes < fileA.length()) {
            int readBytes = streamA.read(bufferA);
            streamB.read(bufferB);

            if (readBytes == 0) break;

            MessageDigest digestA = MessageDigest.getInstance(CHECKSUM_ALGORITHM);
            MessageDigest digestB = MessageDigest.getInstance(CHECKSUM_ALGORITHM);

            digestA.update(bufferA, 0, readBytes);
            digestB.update(bufferB, 0, readBytes);

            if (!MessageDigest.isEqual(digestA.digest(), digestB.digest()))
            {
                closeStreams(streamA, streamB);
                return false;
            }

            totalReadBytes += readBytes;
        }
        closeStreams(streamA, streamB);
        return true;
    } finally {
        closeStreams(streamA, streamB);
    }
}

public static void closeStreams(Closeable ...streams) {
    for (int i = 0; i < streams.length; i++) {
        Closeable stream = streams[i];
        closeStream(stream);
    }
}
public static boolean compareFilesLength(String filePathA, String filePathB) {
    File fileA = new File(filePathA);
    File fileB = new File(filePathB);

    return fileA.length() == fileB.length();
}
private static void closeStream(Closeable stream) {
    try {
        stream.close();
    } catch (IOException e) {
        // ignore exception
    }
}

Your choice, but having an utility class with that functionality that can be reused is better imho. 您的选择,但是具有可以重用的功能的实用程序类是更好的imho。

Good luck and have fun. 祝好运并玩得开心点。

Personally I would do the opposite. 就个人而言,我会做相反的事情。 Surely you need a way to compare two of these data structure in the Java world - so the test would read from the file, build the DS, do its processing, and then assert it's equal to an "expected" DS you set up in your test. 当然,您需要一种方法来比较Java世界中的这两种数据结构 - 因此测试将从文件读取,构建DS,进行处理,然后断言它等于您在自己设置的“预期”DS测试。

(using JUnit4) (使用JUnit4)

@Test
public void testProcessingDoesWhatItShould() {
    final DataStructure original = readFromFile(filename);
    final DataStructure actual = doTheProcessingYouNeedToDo(original);
    final DataStructure expected = generateMyExpectedResult();

    Assert.assertEquals("data structure", expected, actual);
}

If this DS is a simple Java Bean. 如果这个DS是一个简单的Java Bean。 then you can use EqualsBuilder from Apache Commons to compare 2 objects. 然后你可以使用Apache Commons的EqualsBuilder来比较2个对象。

compare bytes loaded from file system and bytes you are going to write file system 比较从文件系统加载的字节数和要写入文件系统的字节数

pseudo code 伪代码

byte[]  loadedBytes = loadFileContentFromFile(file) // maybe apache commons IOUtils.toByteArray(InputStream input) 

byte[]  writeBytes = constructBytesFromDataStructure(dataStructure)

Assert.assertTrue(java.util.Arrays.equals(writeBytes ,loadedBytes));

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

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