Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9137

JUnit 5 conditional test execution

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • groovy-test-junit5

    Description

      JUnit5 is planning to deprecate script-based execution conditions. We should provide the equivalent - but for just Groovy.

      Documentation (at least until removed) for what is currently supported:
      https://junit.org/junit5/docs/current/user-guide/#writing-tests-conditional-execution-scripts

      Issue to deprecate:
      https://github.com/junit-team/junit5/issues/1882

      PR to deprecate:
      https://github.com/junit-team/junit5/pull/1888

      Here is an example showing current support:

      import org.junit.jupiter.api.*
      import org.junit.jupiter.api.condition.*
      import java.util.stream.Stream
      import static org.junit.jupiter.api.Assertions.assertTrue
      // next 4 only for extra logging
      import java.util.logging.ConsoleHandler
      import java.util.logging.Level
      import java.util.logging.Logger
      import org.junit.platform.launcher.listeners.LoggingListener
      
      class MyTest {
        @BeforeAll
        static void init() {
      
          def logger = Logger.getLogger(LoggingListener.name)
          logger.level = Level.FINE
      //    logger.addHandler(new ConsoleHandler(level: Level.FINE))
        }
      
        @EnabledIf(engine = "groovy", value = "System.getProperty('java.specification.version') != '9'")
        @Test
        void streamSum() {
          assertTrue(Stream.of(1, 2, 3)
            .mapToInt(i -> i)
            .sum() > 5, () -> "Sum should be greater than 5")
        }
      
        @DisabledIf(engine = "groovy", value = "junitDisplayName.matches('.*[24].*')") // disable even
        @RepeatedTest(value=5, name = "{displayName} {currentRepetition}/{totalRepetitions}")
        void streamSumRepeated() {
          assert Stream.of(1, 2, 3).mapToInt(i -> i).sum() == 6
        }
      }
      // JUnit5 launcher: passed=4, failed=0, skipped=2, time=14ms
      

      We could consider accepting a String (and evaluate in a new shell which would enable the annotation to be used in non-Groovy source files provided Groovy was on the classpath) and/or just support Groovy test scripts in a fashion similar to Spock/GContracts (I'd probably make this the default if both were supported with a script="...some groovy code..." option for the former case if supported):

      @EnabledIf({ System.getProperty('java.specification.version') != '9' })
      @DisabledIf({ junitDisplayName.matches('.*[24].*') }) // disable even
      

      Examples show support for one of the existing binding variables 'junitDisplayName'. We could consider maintaining support for that information.

      Also be aware of the following issue (we'll need to assess how it impacts us down the track and document appropriately):
      https://github.com/sormuras/junit5-class-vs-module-path

      Attachments

        Activity

          People

            Unassigned Unassigned
            paulk Paul King
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: