TestNG 테스트 사례에 클래스 전체 그룹을 지정할 수 있나요?
문제
TestNG에 데이터베이스 테스트를 나타내는 기본 클래스가 있고 이 클래스에서 확장되는 모든 클래스가 "db-test" 그룹에 속하도록 지정하고 싶지만 이것이 가능하지 않은 것으로 나타났습니다.@Test 주석을 사용해 보았습니다.
@Test(groups = { "db-test" })
public class DBTestBase {
}
그러나 @Test 주석이 여러 메서드를 테스트로 만들려고 시도하고 테스트가 실행될 때 Eclipse에 경고/오류가 나타나기 때문에 이는 작동하지 않습니다.
그래서 테스트를 비활성화하려고 시도하여 최소한 그룹이 할당되었습니다.
@Test(enabled = false, groups = { "db-test" })
public class DBTestBase {
}
하지만 @BeforeTest(및 기타 유사한 주석)도 비활성화됩니다...물론 내가 원하는 것은 아니다.
클래스에 특정 유형의 그룹에 속하도록 주석을 추가하는 방법을 원하지만 TestNG에서는 불가능해 보입니다.누구든지 다른 아이디어가 있나요?
해결책
답은 관습을 통해서이다 org.testng.IMethodSelector:
그것은 include메소드() 주석이 없는 공개 메서드와 같이 원하는 메서드를 제외할 수 있습니다.
그러나 관세를 등록하려면 자바 MethodSelector에 추가해야 합니다. XML테스트 TestRunner에서 관리하는 인스턴스입니다. 즉, 자체 인스턴스가 필요합니다. 맞춤 TestRunner.
그러나 사용자 정의 TestRunner를 구축하려면 테스트러너팩토리,를 통해 -테스트 실행 공장 옵션.
그러나 -testrunfactory는 결코 고려되지 않습니다. 테스트NG 수업...따라서 사용자 정의 TestNG 클래스도 정의해야 합니다.
- configure(Map) 메소드를 재정의하려면,
- 그래서 실제로 TestRunnerFactory를 설정할 수 있습니다
- 사용자 정의 TestRunner를 빌드할 TestRunnerFactory,
- XMLTest 인스턴스에 사용자 정의 XMLMethodSelector를 설정하는 TestRunner
- 사용자 정의 IMethodSelector를 빌드할 XMLMethodSelector
- 선택한 TestNG 메서드를 제외하는 IMethodSelector!
좋아요...그것은 악몽이다.하지만 코드 챌린지이기도 하므로 조금 어려울 것 같습니다. ;)
모든 코드는 다음에서 확인할 수 있습니다. DZone 스니펫.
코드 챌린지의 경우 평소와 같이:
- 하나의 Java 클래스(그리고 꽤 많은 내부 클래스)
- 클래스를 'source/test' 디렉토리에 복사하여 붙여넣습니다(패키지는 'test'이므로).
- 실행해 보세요(인수 필요 없음)
Mike Stone의 업데이트:
제가 하게 된 일과 꽤 비슷해 보이기 때문에 이를 받아들이겠습니다. 하지만 제가 한 일도 추가해야겠다고 생각했습니다.
기본적으로 테스트(및 기타) 주석의 그룹 속성처럼 작동하는 그룹 주석을 만들었습니다.
그런 다음 IAnnotationTransformer를 사용하여 정의된 모든 테스트 및 테스트 클래스를 살펴본 다음 테스트를 수정하여 그룹을 추가하는 GroupsAnnotationTransformer를 만들었습니다. 이는 그룹 제외 및 포함과 완벽하게 작동합니다.
새로운 주석 변환기를 사용하도록 빌드를 수정하면 모든 것이 완벽하게 작동합니다!
잘...한 가지 주의할 점은 테스트가 아닌 메서드에 그룹을 추가하지 않는다는 것입니다.왜냐하면 제가 이 작업을 수행했을 때 무엇이든 변환할 수 있는 또 다른 주석 변환기가 있었지만 어떤 이유로든 제가 사용하고 있던 TestNG에는 포함되지 않았기 때문입니다...따라서 주석이 달린 전/후 메소드를 AlwaysRun=true로 설정하는 것이 좋습니다...그것은 나에게 충분합니다.
최종 결과는 다음과 같습니다.
@Groups({ "myGroup1", "myGroup2"})
public class MyTestCase {
@Test
@Groups("aMethodLevelGroup")
public void myTest() {
}
}
그리고 저는 트랜스포머가 서브클래싱 등 모든 기능을 사용하도록 만들었습니다.
다른 팁
TestNG는 @Test 주석이 있는 클래스의 모든 공개 메서드를 실행합니다.TestNG가 비공개로 실행되는 것을 원하지 않는 메소드를 변경할 수도 있습니다.
제가 보기에는 다음과 같습니다 코드 챌린지 (커뮤니티 위키 게시물):
다음을 사용하지 않고 'aGlobalGroup' 그룹에서 확장 클래스의 모든 테스트 메서드를 실행하는 방법:
- 확장 클래스 자체에 'aGlobalGroup' 그룹을 지정합니까?
- 확장 클래스의 주석이 없는 공개 메소드를 테스트합니까?
첫 번째 대답은 쉽습니다.
기본 클래스 수준에 TestNG(groups = { "aGlobalGroup" }) 클래스를 추가합니다.
해당 그룹은 기본 클래스와 확장 클래스의 모든 공용 메서드에 적용됩니다.
하지만:테스트가 아닌 공개 메서드(TestNG 주석 없음)도 해당 그룹에 포함됩니다.
도전:TestNG가 아닌 메서드를 포함하지 마세요.
@Test(groups = { "aGlobalGroup" })
public class Base {
/**
*
*/
@BeforeClass
public final void setUp() {
System.out.println("Base class: @BeforeClass");
}
/**
* Test not part a 'aGlobalGroup', but still included in that group due to the class annotation. <br />
* Will be executed even if the TestNG class tested is a sub-class.
*/
@Test(groups = { "aLocalGroup" })
public final void aFastTest() {
System.out.println("Base class: Fast test");
}
/**
* Test not part a 'aGlobalGroup', but still included in that group due to the class annotation. <br />
* Will be executed even if the TestNG class tested is a sub-class.
*/
@Test(groups = { "aLocalGroup" })
public final void aSlowTest() {
System.out.println("Base class: Slow test");
//throw new IllegalArgumentException("oups");
}
/**
* Should not be executed. <br />
* Yet the global annotation Test on the class would include it in the TestNG methods...
*/
public final void notATest() {
System.out.println("Base class: NOT a test");
}
/**
* SubClass of a TestNG class. Some of its methods are TestNG methods, other are not. <br />
* The goal is to check if a group specify in the super-class will include methods of this class. <br />
* And to avoid including too much methods, such as public methods not intended to be TestNG methods.
* @author <a href="http://stackoverflow.com/users/6309/vonc">VonC</a>
*/
public static class Extended extends Base
{
/**
* Test not part a 'aGlobalGroup', but still included in that group due to the super-class annotation. <br />
* Will be executed even if the TestNG class tested is a sub-class.
*/
@Test
public final void anExtendedTest() {
System.out.println("Extended class: An Extended test");
}
/**
* Should not be executed. <br />
* Yet the global annotation Test on the class would include it in the TestNG methods...
*/
public final void notAnExtendedTest() {
System.out.println("Extended class: NOT an Extended test");
}
}
유연성을 최대화할 수 있도록 메서드 수준에서 @Test 주석을 지정할 수 있습니다.
public class DBTestBase {
@BeforeTest(groups = "db-test")
public void beforeTest() {
System.out.println("Running before test");
}
public void method1() {
Assert.fail(); // this does not run. It does not belong to 'db-test' group.
}
@Test(groups = "db-test")
public void testMethod1() {
Assert.assertTrue(true);
}
}
이것이 당신에게 효과가 있습니까? 아니면 귀하의 질문에서 뭔가가 누락되었습니다.