【问题标题】:writing java testng test cases编写 java testng 测试用例
【发布时间】:2011-09-29 08:13:34
【问题描述】:

我从 Java 和 testng 测试用例开始。

我需要编写一个类,它从文件中读取数据并创建一个内存数据结构,并使用该数据结构进行进一步处理。我想测试一下,这个 DS 是否被正确填充。这将要求将 DS 转储到文件中,然后将输入文件与转储文件进行比较。是否有任何可用于文件匹配的 testNG 断言?这是一种常见的做法吗?

【问题讨论】:

    标签: java unit-testing testng


    【解决方案1】:

    我认为比较数据本身而不是写出的数据会更好。

    所以我会在类中编写一个方法来返回这个数据结构(我们称之为getDataStructure()),然后编写一个单元测试来与正确的数据进行比较。

    这只需要在您的数据结构类中使用正确的equals() 方法并执行以下操作:

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

    当然如果你需要将数据结构写到文件中,那么你可以分别测试序列化和反序列化。

    【讨论】:

      【解决方案2】:

      文件比较/匹配可以提取到实用方法或类似的东西。 如果您只需要它来进行测试,则可以使用 jUnit 插件 http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html

      如果您需要在测试环境之外进行文件比较,您可以使用这个简单的功能

          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
          }
      }
      

      您的选择,但恕我直言,拥有一个具有该功能的实用程序类会更好。

      祝你好运,玩得开心。

      【讨论】:

        【解决方案3】:

        我个人会做相反的事情。当然,您需要一种方法来比较 Java 世界中的这两种数据结构——因此测试将从文件中读取,构建 DS,进行处理,然后断言它等于您在您的设置中设置的“预期”DS测试。

        (使用 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);
        }
        

        【讨论】:

          【解决方案4】:

          如果这个 DS 是一个简单的 Java Bean。那么您可以使用来自 Apache Commons 的 EqualsBuilder 来比较 2 个对象。

          【讨论】:

            【解决方案5】:

            比较从文件系统加载的字节数和要写入文件系统的字节数

            伪代码

            byte[]  loadedBytes = loadFileContentFromFile(file) // maybe apache commons IOUtils.toByteArray(InputStream input) 
            
            byte[]  writeBytes = constructBytesFromDataStructure(dataStructure)
            
            Assert.assertTrue(java.util.Arrays.equals(writeBytes ,loadedBytes));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-10-10
              • 2020-01-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多