【问题标题】:Javolution test patterns, dos and don'tsJavolution 测试模式,注意事项
【发布时间】:2008-12-11 09:33:04
【问题描述】:

在为Javolution 测试编写测试时,有哪些模式和注意事项?我特别想知道:

  • TestCase.execute() 不允许抛出异常。如何对付他们?重新抛出 RuntimeException 或存储在变量中并在 TestCase.validate() 中断言或其他什么?
  • 是否有任何图形运行程序可以向您显示失败的测试,即在 Eclipse 中?也许有人写了一个 JUnit-Wrapper 以便我可以使用 Eclipse JUnit Runner?

【问题讨论】:

    标签: java unit-testing


    【解决方案1】:

    javadoc 和 javolution 源提供了一些示例和设计原理。 另见an article on serverside

    Javolution 测试只包含一个测试,测试代码的执行与验证分离为不同的方法 execute() 和 validate()。因此,相同的测试类可用于回归测试和速度测试(省略了对 validate() 的调用)。此外,许多测试的执行都是可并行化的。

    这种分离的一个缺点是:你会消耗更多的内存,因为在调用 validate() 之前需要保存执行代码的执行结果。 (在 tearDown 中释放这些结果可能是个好主意。) 如果 validate 来自与 exercise 不同的类,那么可能很难调试失败。

    【讨论】:

      【解决方案2】:

      您可以通过使用以下 JUnit 适配器并在 eclipse 中运行它来获得某种图形测试运行程序。您可以单独启动/调试失败的测试。不幸的是,图形表示不包含有关实际测试的任何内容 - 它仅显示数字 [0]、[1] 等。

      @RunWith(Parameterized.class) 公共类 JavolutionJUnit4Adapter {

      protected final javolution.testing.TestCase test;
      
      public JavolutionJUnit4Adapter(javolution.testing.TestCase testcase) {
          this.test = testcase;
      }
      
      @org.junit.Test
      public void executeTest() throws Exception {
          enter(REGRESSION);
          try {
              new javolution.testing.TestSuite() {
                  @Override
                  public void run() {
                      test(test);
                  }
              }.run();
          } finally {
              exit();
          }
      }
      
      @Parameters
      public static Collection<javolution.testing.TestCase[]> data() {
          javolution.testing.TestSuite fp = new WhateverSuiteYouWantToRun();
          List<javolution.testing.TestCase> tests = fp.getTestCases();
          Collection<javolution.testing.TestCase[]> res = new ArrayList<javolution.testing.TestCase[]>();
          for (javolution.testing.TestCase t : tests) {
              res.add(new javolution.testing.TestCase[] { t });
          }
          return res;
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2017-06-15
        • 2011-06-06
        • 1970-01-01
        • 2020-01-19
        • 2015-10-24
        • 1970-01-01
        • 2018-12-27
        • 2010-11-16
        • 1970-01-01
        相关资源
        最近更新 更多