JUnit 5

2files
4min read
Sheet 33 of 47 · Automation

Filess08-a-flight-afk.yml · src/test/resources/junit-platform.properties

Files08-a-flight-afk.ymlFilesrc/test/resources/junit-platform.propertiesThe engine CI mode runs on, inside a plugin's own tests. Every top level group of a scenario file shows up as its own test in the IDE and in the build's test report, and steps can be written in Java next to the plugin's unit tests. The bots join a server that is already running, the same one CI mode would start.

Adding it

dependencies {
  testImplementation 'com.ignitedev:IgniteTestTool-junit:1.3.0'
  testImplementation 'org.junit.jupiter:junit-jupiter'
  testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
 
tasks.named('test') {
  useJUnitPlatform()
}

IgniteTestTool-junit brings the engine with it as com.ignitedev:IgniteTestTool: the tool without its window, with its own gson, snakeyaml, adventure and Mongo driver moved under its package so they never clash with the versions a plugin's tests carry. Logging goes through SLF4J, so the test classpath's own backend prints it.

A scenario file as tests

@IgniteTest(port = 25600, version = "1.21.10", scenarios = "src/test/itt")
class FlightTest {
 
  @ScenarioTest("s08-a-flight-afk.yml")
  void flight(GroupResult group) {
  }
}
  • The file runs once. Each top level group is a test named after the group, so 8.5 flying into a claim takes flight is a line in the test tree.
  • A group that passed runs the method body with its GroupResult, for any check of your own: its steps, their messages and times.
  • A group that failed fails its test with the step that broke, the bot it ran on and the message, like 3.1 chat [TstAnn]: no chat matching /Flight on/.
  • A group the run never got to is skipped, with the failure that stopped the run in the reason.
  • A file with no groups is one test. A matrix file prints each row's groups with [row] in front.

Includes resolve from the lib folder next to the file, the same as in CI mode.

Steps in Java

@IgniteTest(port = 25600)
class FlyTest {
 
  @Test
  void flyInWilds(Ignite ignite) {
    ignite.verify(scenario("fly", "TstAnn")
        .join()
        .group("8.1 /fly in the wilds", steps -> steps
            .chat("/fly")
            .expectChat("Flight on", Duration.ofSeconds(5)))
        .as("TstBen", ben -> ben.expectNoChat("Flight on", Duration.ofSeconds(1)))
        .cleanup(steps -> steps.quit()));
  }
}

ScenarioBuilder builds the same steps a scenario file holds, so a step written in Java and the same step in YAML run alike.

MethodStep
join(), quit()join, quit
chat(text)chat
pause(duration)wait
expectChat(regex), expectChat(regex, timeout)expectChat
expectNoChat(regex, window)expectNoChat
expectWindow(title), expectNoWindow()expectWindow, expectNoWindow
click(regex), clickSlot(slot)click by name or by slot
close()close
expectLog(source, regex)expectLog
action(name, params)action
http(url, body)http
group(name, steps), loop(times, steps)group, loop
as(bot, steps)the steps inside run as that bot
step(name, params)any other step by its name in the file format, e.g. step("expectSlot", Map.of("item", "dirt", "inventory", true))
optional(), timeout(duration)change the step added last
cleanup(steps)finally, runs whatever failed before
var(key, value), continueOnFailure()the file's vars and continueOnFailure

Ignite is what a test method takes:

  • run(scenario) runs it and hands back the ScenarioReport.
  • verify(scenario) does the same and fails the test when the scenario failed, with every failed step, where its bot was and the last 40 lines of the transcript.
  • load("s08-a-flight-afk.yml") reads a scenario file into scenarios to run or to change first.

A test can take ScenarioEngine instead for the bare engine. IgniteAssertions.assertPassed takes a report or a group result.

Logs

Config keyitt.logEvery run writes its transcript to build/itt/<test class>/<test>.log: each step as it starts and ends, every chat line any bot got and the server logs named in serverLogs, in order with times, the same log CI mode writes with --logs. The path is published as an itt.log report entry, so it shows up next to the test in the IDE and in the build report.

Configuration

Filesrc/test/resources/junit-platform.propertiesConfiguration parameters win over @IgniteTest, so CI can point the same tests at another server. Set them in src/test/resources/junit-platform.properties, or as systemProperty on the Gradle test task. A plain -D on the Gradle command line stays in Gradle's own JVM and never reaches the tests.

Parameter@IgniteTestMeaning
itt.hosthostwhere bots join, default localhost
itt.portportdefault 25565
itt.versionversionthe version bots speak, like --bot-version
itt.scenariosscenariosfolder @ScenarioTest and load paths are relative to
itt.serverLogsserverLogslabel=path logs that go into every transcript, comma separated, like --log
itt.logswhere the run logs go, default build/itt
itt.dataDirthe engine's data folder, default a fresh temp folder
itt.engine.factoryan EngineFactory class that opens the engine, for a fake one in the tests of your tests
itt.host=127.0.0.1
itt.port=25600
itt.version=1.21.10
itt.serverLogs=proxy=../rig/proxy/logs/latest.log,towny=../rig/towny/logs/latest.log

Runs and bots

  • One engine serves the whole test run, opened on the first test that needs it and closed when the run ends.
  • Scenarios take turns and the bots disconnect after every one, so each starts from a clean join and tests can run in parallel without bots meeting each other.
  • @IgniteTest tags the class ignite, so excludeTags 'ignite' keeps bot tests out of a quick unit test run.

License

The same as CI mode: ITT_LICENSE takes the license's shared CI slot, without it the key saved on this computer counts. A run that is not licensed fails every test with the reason, before any bot joins.

Not in it yet

  • Server errors in the server log do not fail a JUnit test the way they fail a CI mode scenario. Where an error has to fail the run, run the files in CI mode with --server-log.
  • The engine does not start the server. Start it before the tests, or run them from a CI job that does.

Loading the index

Enter openUp Down moveEsc close