Skip to content

Commit

Permalink
Add scripted test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Podsiadlo committed Dec 29, 2021
1 parent b25db4b commit a13b847
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ target
/\.manager/
/\.idea/
/\.idea_modules/
.metals/
.bloop/
.bsp/
.vscode/
6 changes: 6 additions & 0 deletions src/sbt-test/simple/check-test-selector/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name := "test-project"

scalaVersion := "2.13.7"

libraryDependencies += "com.github.sbt" % "junit-interface" % sys.props("plugin.version") % "test"
libraryDependencies += "org.scala-sbt" % "test-agent" % "1.5.5" % Test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.6.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import com.novocode.junit.JUnitFramework
import com.novocode.junit.JUnitFingerprint

import org.junit.Test
import org.junit.Assert._

import sbt.testing._
import scala.collection.mutable.ArrayBuffer

/**
* Check if TestSelector's are correctly handled by JUnitRunner.
* Execute prepared TaskDef's using manually created instances of sbt.testing.{Framework and Runner}.
*/
class CheckTestSelector {
val framework = new JUnitFramework();
val runner = framework.runner(
Array.empty[String],
Array.empty[String],
this.getClass().getClassLoader()
);

private def getEventHandler(): (ArrayBuffer[String], EventHandler) = {
val executedItems = new scala.collection.mutable.ArrayBuffer[String]
val eventHandler = new EventHandler {
override def handle(event: Event) =
if (event.status() == Status.Success) {
executedItems.addOne(event.fullyQualifiedName())
}
}
(executedItems, eventHandler)
}

private def getTaskDefs(selectors: Array[Selector]): Array[TaskDef] = {
Array(
new TaskDef("a.b.MyTestSuite", new JUnitFingerprint(), false, selectors)
)
}

@Test
def runAllViaSuiteSelector() {
val selectors = Array[Selector](
new SuiteSelector
)
val taskDefs = Array(
new TaskDef("a.b.MyTestSuite", new JUnitFingerprint(), false, selectors)
)

val tasks = runner.tasks(taskDefs)
assertEquals(tasks.size, 1)
val task = tasks(0)

val (executedItems, eventHandler) = getEventHandler()

task.execute(eventHandler, Nil.toArray)
assertArrayEquals(
Array[Object]("a.b.MyTestSuite.testBar", "a.b.MyTestSuite.testFoo"),
executedItems.toArray[Object]
)
}

@Test
def runAllViaTestSelectors() {
val selectors = Array[Selector](
new TestSelector("testFoo"),
new TestSelector("testBar")
)
val taskDefs = getTaskDefs(selectors)

val tasks = runner.tasks(taskDefs)
assertEquals(tasks.size, 1)
val task = tasks(0)

val (executedItems, eventHandler) = getEventHandler()

task.execute(eventHandler, Nil.toArray)
assertArrayEquals(
Array[Object]("a.b.MyTestSuite.testBar", "a.b.MyTestSuite.testFoo"),
executedItems.toArray[Object]
)
}

@Test
def runOnlyOne() {
val selectors = Array[Selector](
new TestSelector("testFoo")
)
val taskDefs = getTaskDefs(selectors)

val tasks = runner.tasks(taskDefs)
assertEquals(tasks.size, 1)
val task = tasks(0)

val (executedItems, eventHandler) = getEventHandler()

task.execute(eventHandler, Nil.toArray)
assertArrayEquals(
Array[Object]("a.b.MyTestSuite.testFoo"),
executedItems.toArray[Object]
)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package a.b

import org.junit.Test
import org.junit.Assert.assertEquals

class MyTestSuite {

@Test
def testFoo(): Unit = {
assertEquals("Test should pass", true, true)
}

@Test
def testBar(): Unit = {
assertEquals("Test should pass", true, true)
}
}
2 changes: 2 additions & 0 deletions src/sbt-test/simple/check-test-selector/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# make sure the unit test passes
> test

0 comments on commit a13b847

Please sign in to comment.