조건부 테스트 : 일부 테스트를 실행하지 않는 것을 말한다.
필요성
- 예를 들어 테스트하려는 메소드가 이미 문제가 있는 것으로 확인된 경우, 문제를 해결할 때까지 테스트를 동작하지 않도록 할 때 쓸 수 있다.
- 주석으로 처리하는 방법도 있지만 주석으로 처리할 시와 달리 아래와 같은 방법을 사용하면 테스트 결과에 무시된 테스트로 표시된다.
@Disabled("주석 메세지") : 이 어노테이션이 붙은 테스트 메소드는 실행되지 않는다.
@EnabledOnOs(OS.*) : *에 따른 운영체제에만 해당 테스트를 실행해준다. {}로 감쏴서 여러 개의 운영체제를 설정할 수 있다.
...
@Test
@EnabledOnOs({OS.WINDOWS, OS.MAC})
void testArrayEquals() {
//set up
String[] strings = {"A", "B", "C"};
//execute
String[] firstThreeSmallLettersOfAlphabet = demoUtils.getFirstThreeLettersOfAlphabet();
//assert
assertArrayEquals(strings, firstThreeSmallLettersOfAlphabet, "Should be same");
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX})
void testIterableEquals() {
//set up
List<String> list = List.of("choi", "jae", "hyuk");
//execute
List<String> name = demoUtils.getName();
//assert
assertIterableEquals(list, name, "Should be same");
}
@Test
@EnabledOnOs(OS.WINDOWS)
void testLinesMatch() {
//set up
List<String> list = List.of("choi", "jae", "hyuk");
//execute
List<String> name = demoUtils.getName();
//assert
assertLinesMatch(list, name, "Should be same");
}
@Test
@EnabledOnOs(OS.MAC)
void throwExceptionIfLessThanZero() {
//set up
int n1 = -1;
int n2 = 1;
//execute & assert
assertThrows(Exception.class, () -> demoUtils.throwExceptionIfLessThanZero(n1));
assertDoesNotThrow(() -> demoUtils.throwExceptionIfLessThanZero(n2));
}
@Test
@Disabled("not test")
void checkTimeout() {
//set up
long sec = 3;
//execute & assert
assertTimeoutPreemptively(Duration.ofSeconds(sec), () -> demoUtils.checkTimeout());
}
@EnabledOnJre(JRE.*) : *에 해당하는 jre 버전일 때 테스트가 실행된다.
@EnabledForJreRange(min=JRE.*, max=JRE.*) : min과 max을 포함한 사이의 jre 버전일 때 테스트가 실행된다. min과 max는 디폴트 값이 설정되어 있기에 생략 가능하다.
...
@Test
@Order(1)
@EnabledOnJre(JRE.JAVA_17)
void add() {
//execute
int actual = demoUtils.add(2, 4);
//assert
Assertions.assertEquals(6, actual, "2+4 must be 6");
Assertions.assertNotEquals(7, actual, "2+4 must not be 7");
}
@Test
@Order(2)
@EnabledOnJre(JRE.JAVA_10)
void checkNull() {
//set up
DemoUtils demoUtils = new DemoUtils();
String string1 = null;
String string2 = "choi";
//execute
Object obj1 = demoUtils.checkNull(string1);
Object obj2 = demoUtils.checkNull(string2);
//assert
Assertions.assertNull(obj1, "Object must be null");
Assertions.assertNotNull(obj2, "Object must not be null");
}
@Test
@EnabledForJreRange(min=JRE.JAVA_17, max = JRE.JAVA_20)
void testSameAndNotSame() {
//set up
String str = "choi";
//execute
String str2 = demoUtils.getAcademy();
String str3 = demoUtils.getAcademyDuplicate();
//assert
Assertions.assertSame(str2, str3, "Academy must be the same");
Assertions.assertNotSame(str, str2, "Academy must not be the same");
}
@Test
@EnabledForJreRange(min=JRE.JAVA_18)
void isGreater() {
//set up
int n1 = 10;
int n2 = 5;
//execute
boolean result1 = demoUtils.isGreater(n1, n2);
boolean result2 = demoUtils.isGreater(n2, n1);
//assert
assertTrue(result1, "Should be greater than " + n1 + " and " + n2);
assertFalse(result2, "Should be not greater than " + n1 + " and " + n2);
}
...
@EnabledIfEnvironmentVariable(named="name", matches="matches") : 환경 변수의 이름과 값을 통해 일치하면 테스트를 실행하게 해주는 어노테이션이다.
@EnabledIfEnvironmentVariable({@EnabledIfEnvironmentVariable(named="name", matches="matches"), ...}) : 환경변수의 값을 여러 개 적용시킬 수 있는 어노테이션이다.
@Test
//@EnabledIfEnvironmentVariable(named = "test", matches = "correct")
@EnabledIfEnvironmentVariables({
@EnabledIfEnvironmentVariable(named = "text", matches = "uncorrect"),
@EnabledIfEnvironmentVariable(named = "test", matches = "correct")})
void testArrayEquals() {
//set up
String[] strings = {"A", "B", "C"};
//execute
String[] firstThreeSmallLettersOfAlphabet = demoUtils.getFirstThreeLettersOfAlphabet();
//assert
assertArrayEquals(strings, firstThreeSmallLettersOfAlphabet, "Should be same");
}
cf)환경 변수를 설정하는 방법은 아래와 같다.
'Spring Boot > testing' 카테고리의 다른 글
매개변수화된 테스트 (0) | 2024.11.11 |
---|---|
TDD(Test-Driven Development) (0) | 2024.11.11 |
JaCoCO를 통한 코드 커버리지 리포트 생성 (0) | 2024.11.10 |
코드 커버리지 in JUnit (0) | 2024.11.09 |
테스트 순서 지정 (0) | 2024.11.09 |