Loading ...
Sorry, an error occurred while loading the content.
 

Re: [junit] Testing test infastructure

Expand Messages
  • David Saff
    Steve, There s two solutions I know, and neither is super. The first, which we do with the JUnit self-tests, is to declare a Suite and hand-include the tests
    Message 1 of 6 , Feb 1, 2010
      Steve,

      There's two solutions I know, and neither is super.

      The first, which we do with the JUnit self-tests, is to declare a
      Suite and hand-include the tests that should actually be run.

      The second, I just realized, is made a little easier by Rules. Start
      with this rule:

      public class EnableDuringRun extends ExternalResource {
      private boolean enabled = false;
      protected void before() { enabled = true; }
      protected void after() { enabled = false; }
      public boolean isEnabled() { return enabled; }

      public MethodRule skipUnlessEnabled() {
      return new MethodRule() {
      Statement apply(Statement base, Framework method, Object target) {
      if (enabled) {
      return base;
      } else {
      return new Statement() { public void evaluate() {} };
      }
      }
      };
      }
      }

      And then your test classes become:

      public class OuterTest {
      @Rule public EnableDuringRun runInners = new EnableDuringRun();

      public static class InnerTest {
      @Rule public MethodRule onlyAsExample = runInners.skipUnlessEnabled();
      }

      @Test public void foo() { ... }
      }

      This will allow the inner tests to be detected and run, but will have
      them simply pass unless they're run within the context of an outer
      class's test method.

      Make sense?

      David

      On Sun, Jan 31, 2010 at 12:00 PM, steve.ebersole <steve@...> wrote:
      > Hey all,
      >
      > I am developing some tests with JUnit 4.7.  It includes a bunch of standard stuff that I refactored out into rules and runners and 2 base test classes.  I want to test theses pieces of the "testing infrastructure", so I wrote some basic JUnit tests that themselves invoke JUnitCore.runClasses directly passing inner classes as the tests.  That part actually works great!  The issue is that when T try to run all the tests, these inner clases are picked up as well.  For example, in http://tinyurl.com/yc6j46y I actually end up getting tests for both FunctionalTestCaseTest and FunctionalTestCaseTest$FixtureTests.
      >
      > This is inside the IDE, when I right click on a package or the test dir and ask it to run tests.  I understand in {build-tool=of-choice-goes-here} I could exclude these inner classes.  Guess I am looking for a general purpose solution that works no matter how the tests are launched.
      >
      > Thanks in advance :)
      >
      >
      >
      > ------------------------------------
      >
      > Yahoo! Groups Links
      >
      >
      >
      >
    • steve.ebersole
      Thanks for the help David. For the Suite approach, where/how would I define the Suite? If you mean to define an AllTests-style class, I ve had problems with
      Message 2 of 6 , Feb 1, 2010
        Thanks for the help David.

        For the Suite approach, where/how would I define the Suite? If you mean to define an AllTests-style class, I've had problems with this in the past in IntelliJ in that if you ask it to "run tests" for a package it will find and run the explicit Suites *and* the individual test classes under that package.

        In the rule approach, though, it will still throw the numbers off (iiuc) which I use in the assertions.
      • Alistair Israel
        ... Um, I m afraid this won t work. The static class InnerTest cannot reference the non-static field OuterTest#runInners. - alistair --
        Message 3 of 6 , Feb 1, 2010
          On Mon, Feb 1, 2010 at 11:33 PM, David Saff <david@...> wrote:

          > And then your test classes become:
          >
          > public class OuterTest {
          >   @Rule public EnableDuringRun runInners = new EnableDuringRun();
          >
          >   public static class InnerTest {
          >     @Rule public MethodRule onlyAsExample = runInners.skipUnlessEnabled();
          >   }
          >
          >   @Test public void foo() { ... }
          > }

          Um, I'm afraid this won't work. The static class InnerTest cannot
          reference the non-static field OuterTest#runInners.

          - alistair
          --
          http://alistairisrael.wordpress.com
        • David Saff
          Oops. However, easily, if somewhat more messily, fixed: public class OuterTest { private static EnableDuringRun runInners = new EnableDuringRun(); @Rule
          Message 4 of 6 , Feb 1, 2010
            Oops. However, easily, if somewhat more messily, fixed:

            public class OuterTest {
            private static EnableDuringRun runInners = new EnableDuringRun();

            @Rule public EnableDuringRun instanceRule = runInners;

            public static class InnerTest {
            @Rule public MethodRule onlyAsExample = runInners.skipUnlessEnabled();
            }

            @Test public void foo() { ... }
            }

            On Mon, Feb 1, 2010 at 7:15 PM, Alistair Israel <aisrael@...> wrote:
            > On Mon, Feb 1, 2010 at 11:33 PM, David Saff <david@...> wrote:
            >
            >> And then your test classes become:
            >>
            >> public class OuterTest {
            >>   @Rule public EnableDuringRun runInners = new EnableDuringRun();
            >>
            >>   public static class InnerTest {
            >>     @Rule public MethodRule onlyAsExample = runInners.skipUnlessEnabled();
            >>   }
            >>
            >>   @Test public void foo() { ... }
            >> }
            >
            > Um, I'm afraid this won't work. The static class InnerTest cannot
            > reference the non-static field OuterTest#runInners.
            >
            > - alistair
            > --
            > http://alistairisrael.wordpress.com
            >
            >
            > ------------------------------------
            >
            > Yahoo! Groups Links
            >
            >
            >
            >
          • David Saff
            Steve, We create a Suite using: @RunWith(Suite.class) @SuiteClasses({A.class, B.class, ...}) public class MySuite {} and then point the IDE to just run the
            Message 5 of 6 , Feb 2, 2010
              Steve,

              We create a Suite using:

              @RunWith(Suite.class)
              @SuiteClasses({A.class, B.class, ...})
              public class MySuite {}

              and then point the IDE to just run the MySuite, rather than gathering
              all the classes in the package.

              David Saff

              On Mon, Feb 1, 2010 at 10:56 AM, steve.ebersole <steve@...> wrote:
              > Thanks for the help David.
              >
              > For the Suite approach, where/how would I define the Suite?  If you mean to define an AllTests-style class, I've had problems with this in the past in IntelliJ in that if you ask it to "run tests" for a package it will find and run the explicit Suites *and* the individual test classes under that package.
              >
              > In the rule approach, though, it will still throw the numbers off (iiuc) which I use in the assertions.
              >
              >
              >
              >
              > ------------------------------------
              >
              > Yahoo! Groups Links
              >
              >
              >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.