λ€μ ν μ€νΈ ν΄λμ€λ₯Ό κ³ λ €νμμμ€.
<strong i="6">@Test</strong>
public abstract class ParentTest {
public void parentTestMethod() {
}
}
@Test(groups = "myGroup")
public class ChildTest extends ParentTest {
public void childTestMethod() {
}
}
μ΄ ν μ€νΈ(ChildTest)λ₯Ό μ€νν λ λ ν μ€νΈ λ©μλ(childTestMethod λ° parentTestMethod)κ° λͺ¨λ 'myGroup' κ·Έλ£Ήμ μν κ²μΌλ‘ μμν©λλ€. λΆλͺ ν μ΄κ²μ μ€μ λμμ΄ μλλλ€(λ²μ 6.4μμ ν μ€νΈλ¨).
μ΄ λ¬Έμ λ₯Ό μ‘°μ¬νκΈ° μν΄ λͺ κ°μ§ μ½λλ₯Ό μμ±νμ΅λλ€.
TestNGMethodFinder testNGMethodFinder = new TestNGMethodFinder(new RunInfo(), new JDK15AnnotationFinder(new DefaultAnnotationTransformer()));
ITestNGMethod[] testMethods = testNGMethodFinder.getTestMethods(ChildTest.class, new XmlTest());
for (ITestNGMethod testMethod : testMethods) {
String[] groups = testMethod.getGroups();
System.out.println(testMethod + " groups:" + Arrays.toString(groups));
}
μ΄ μ½λ μ€νμ μΆλ ₯:
ChildTest.childTestMethod()[pri:0, instance:null] groups:[myGroup]
ParentTest.parentTestMethod()[pri:0, instance:null] groups:[]
TestNG μμ€ μ½λλ₯Ό μ΄ν΄λ³΄λ μ΄ λμμ΄ org.testng.internal.BaseTestMethod#initGroups(Class>) λ©μλμμ λΉλ‘―λ κ²μΌλ‘ 보μ λλ€. μ νν λ§νλ©΄ λ€μκ³Ό κ°μ λ§μ΄ μ΄ λ€μ μ΄μν νλμ μμΈμ΄λΌκ³ μκ°ν©λλ€.
ITestOrConfiguration classAnnotation = (ITestOrConfiguration) getAnnotationFinder().findAnnotation(getMethod().getDeclaringClass(), annotationClass);
μ΄ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ μ΄ λ¬Έμ λ€μκ³Ό κ°μ΄ μμ ν μ μμ΅λλ€.
ITestOrConfiguration classAnnotation = (ITestOrConfiguration) getAnnotationFinder().findAnnotation(getInstance().getClass(), annotationClass);
κ·Έλ¬λ μ΄κ²μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ μ¬λ°λ₯Έ λ°©λ²μΈμ§ νμ€νμ§ μμ΅λλ€. κ²°κ³Όμ μλ―Έλ μκ³ ....
λ¬Έμ κ° μλ κ² κ°μλ° λΆννλ κ·νμ μ μμ΄ μ λλ‘ μλνμ§ μμ΅λλ€. μ£Όμμ΄ μ΄κΈ°ν λ¨κ³μμ ν΄κ²°λκΈ° λλ¬Έμ ν΄κ²°νκΈ°κ° μ½κ° κΉλ€λ‘μ΅λλ€. μ¦, λΆλͺ¨μ @Test μ£Όμμ΄ κ΅¬λ¬Έ λΆμλ λ μμ μ£Όμμ΄ λ λ§μ κ·Έλ£Ήμ μΆκ°νλ€λ μ¬μ€μ λν΄ μ ν μμ§ λͺ»ν©λλ€...
Cedric, μ΄ λ¬Έμ μ λν μ μν νΌλλ°±μ κ°μ¬λ립λλ€. "ν΄κ²°νκΈ°κ° μ‘°κΈ κΉλ€λ‘λ€"κ³ λ§μνμ ¨λλ°, μ΄ λ¬Έμ κ° κ°κΉμ΄ μμΌ λ΄μ ν΄κ²°λ κ°λ₯μ±μ΄ μλ€κ³ μκ°νμλκΉ? κ°λ₯ν ν΄κ²° λ°©λ²μ λν΄ μκ°ν΄ λ³Ό μ μμ΅λκΉ?
λ΄ λ§μ: κ·Έλ£Ή νμΈμ΄ μνλ λ ν μ€νΈ ν΄λμ€κ° μμ§ μΈμ€ν΄μ€νλμ§ μμμΌλ―λ‘ parentTestMethod()λ₯Ό λ³Ό λ μ΄ κ·Έλ£Ήμ ν΄λΉ λ©μλλ₯Ό λ£μ μ μλ ChildTest μΈμ€ν΄μ€κ° μμ΅λλ€.
κ·Έλ£Ή νμΈμ μμ ν μ μ μ΄μ§λ§ μμ μλ λμ μΌλ‘λ§ νμΈν μ μμ΅λλ€.
μ΄κ² λ§μ΄ λΌ?
λλ μ΄λ° νλμ λ³Έ μ μ΄ μλ€. κ·Έλ£Ή νν° μμ΄ ν
μ€νΈλ₯Ό μ€ννλ©΄ λͺ¨λ κ²μ΄ ν΅κ³Όνμ§λ§ κ·Έλ£ΉμΌλ‘ μ€νν λλ μ€ν¨ν©λλ€. μ΄λ€ κ²½μ°μλ λ€λ₯Έ ν
μ€νΈ ν΄λμ€μμ μμν ν
μ€νΈ ν΄λμ€ λλ¬Έμ΄μ§λ§ λ€λ₯Έ κ²½μ°μλ POJO ν
μ€νΈ ν΄λμ€κ° μκ³ @BeforeMethod
λ° @AfterMethod
λ©μλκ° μ λλ‘ νΈμΆλμ§ μλ κ² κ°μ΅λλ€.
BTW, λ΄ μ루μ μ λ¬Όλ‘ λͺ¨λ μν©μμ μλνμ§ μλ μ μΈ κ·Έλ£Ήμ μ¬μ©νλ κ²μ λλ€. @cbeust , μ΄μ λν μ μ ν μ루μ μ λν μμ μ΄ μμμ΅λκΉ?
1λ
μ΄ λμμ§λ§ μ¬μ ν μλνμ§ μμ΅λλ€.
@cbeust IMHO μ΄κ²μ μλ―Έκ° μμ΅λλ€. μμ ν΄λμ€κ° μΆμμ΄λ―λ‘ ν΄λΉ κ·Έλ£Ή νμΈμ ν
μ€νΈ ν΄λμ€μ ꡬ체μ μΈ μΈμ€ν΄μ€λ₯Ό κΈ°λ°μΌλ‘ ν΄μΌ ν©λλ€.
FWIW λλ λ°©κΈ μ΄κ²μ λΆλͺμ³€λ€.
νμ ν΄λμ€ ν μ€νΈ μ£Όμμ κ·Έλ£Ήμ μ¬μ©νλ μ¬μ©μ μ§μ λ©μλ μ νκΈ°λ₯Ό λ§λ€μμ΅λλ€.
/* *****************************************************************************
* Copyright 2017 VMware, Inc. All rights reserved. VMware Confidential
* ****************************************************************************/
package com.vmware.cloud.systemtests.util;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;
import org.testng.annotations.Test;
import org.testng.xml.XmlTest;
/**
* Selector that uses groups in subclass <strong i="6">@Test</strong> annotations to determine if methods should be included
*/
public class SubclassAwareXmlMethodsSelector implements IMethodSelector {
<strong i="7">@Override</strong>
public boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
XmlTest xmlTest = method.getXmlTest();
if (xmlTest == null) {
return method.isAlwaysRun();
}
Class realTestClass = isTestMethod ? method.getTestClass().getRealClass() : method.getRealClass();
Test testClassAnnotation = (Test) realTestClass.getAnnotation(Test.class);
String[] classAnnotationGroups = testClassAnnotation != null ? testClassAnnotation.groups() : new String[0];
List<String> groups = Arrays.asList(ArrayUtils.addAll(classAnnotationGroups, method.getGroups()));
context.setStopped(true);
if (groups.size() == 0 && xmlTest.getIncludedGroups().isEmpty()) {
return true;
} else if (groups.size() > 0 && xmlTest.getExcludedGroups().stream().anyMatch(groups::contains)) {
return false;
} else if (groups.size() > 0 && xmlTest.getIncludedGroups().stream().anyMatch(groups::contains)) {
return true;
} else {
return method.isAlwaysRun();
}
}
<strong i="8">@Override</strong>
public void setTestMethods(List<ITestNGMethod> list) {
}
}