トッカンソフトウェア

JUnit 5 まとめて実行

今回は、複数のJUnitクラスファイルをまとめて実行してみます


まとめて実行

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>TestMvn</groupId>
	<artifactId>TestMvn</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<java.version>11</java.version>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<maven.compiler.source>${java.version}</maven.compiler.source>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-suite</artifactId>
			<version>1.12.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter</artifactId>
			<version>5.12.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

java テストケースまとめ


package test;

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

import test2.Sample2Test;

@Suite
@SelectClasses({

	SampleTest.class

	, Sample2Test.class

})
public class AllTests {

}

SampleTestとSample2Testのテストを連続で実行できます。

java テストケース1


package test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class SampleTest {

	@Test
	void test() {

		System.out.println("SampleTest");

		int i = 1 + 1;
		assertEquals(2, i);
	}

}

java テストケース2


package test2;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class Sample2Test {

	@Test
	void test() {

		System.out.println("Sample2Test");

		int i = 1 + 1;
		assertEquals(2, i);
	}

}

実行結果

SampleTest
Sample2Test


パッケージ指定でテスト実施


package test;

import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectPackages("test2")
public class AllTests {

}

実行結果

Sample2Test


まとめて実行時の前後処理


package test;

import org.junit.platform.suite.api.AfterSuite;
import org.junit.platform.suite.api.BeforeSuite;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

import test2.Sample2Test;

@Suite
@SelectClasses({

		SampleTest.class

		, Sample2Test.class

})
public class AllTests {

	@BeforeSuite
	static void beforeSuite() {

		System.out.println("beforeSuite");
	}

	@AfterSuite
	static void afterSuite() {

		System.out.println("afterSuite");
	}

}

実行結果

beforeSuite
SampleTest
Sample2Test
afterSuite

@BeforeSuiteと@AfterSuiteで全体処理前、後に処理を実行できます。

ページのトップへ戻る