Skip to content

can't run tests with same name multiple times #96

@unkarjedy

Description

@unkarjedy

JUnit supports running tests with the same fully-qualified name several times.
It can be achieved by using some non-standard built-in runners or some custom runners.

Here is the easiest example of such test using org.junit.runners.Suite

build.sbt

name := "junit-with-sbt"
version := "0.1"
scalaVersion := "2.13.6"
// NOTE: by default junit-interface ignores `org.junit.runners.Suite`, we need to disable it
Test / testOptions += Tests.Argument( "--verbosity=3", "--ignore-runners=")
libraryDependencies += ("com.novocode" % "junit-interface" % "0.11" % Test).withSources()

MyCompositeTest.scala:

package example

import junit.framework.{TestCase, TestSuite}
import org.junit.Assert.fail
import org.junit.runner.RunWith
import org.junit.runners.Suite.SuiteClasses

@RunWith(classOf[org.junit.runners.Suite])
@SuiteClasses(Array(
  classOf[example.MyActualTest],
  classOf[example.MyActualTest],
  classOf[example.MyActualTest]
))
class MyCompositeTest extends TestSuite

class MyActualTest extends TestCase {
  def test1_Success(): Unit = {
    println("running success test")
  }

  def test2_Failed(): Unit = {
    println("running failed test")
    fail("some reason")
  }
}

Note that in practice, those tests can have different contexts but have the same name.

Intellij IDEA, for example, successfully runs such tests:
(note, that in total, there were 6 tests run.

image

And thisis what testOnly example.MyCompositeTest outputs:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions