下面列出了怎么用org.testng.ITestNGMethod的API类实例代码及写法,或者点击链接到github查看源代码。
public void onStart(ITestContext context) {
ITestNGMethod[] allTestMethods = context.getAllTestMethods();
if (allTestMethods == null) {
return;
}
if (httpServer == null && hasEverrestJettyListenerTestHierarchy(allTestMethods)) {
httpServer = new JettyHttpServer();
context.setAttribute(JETTY_PORT, httpServer.getPort());
context.setAttribute(JETTY_SERVER, httpServer);
try {
httpServer.start();
httpServer.resetFactories();
httpServer.resetFilter();
RestAssured.port = httpServer.getPort();
RestAssured.basePath = JettyHttpServer.UNSECURE_REST;
} catch (Exception e) {
LOG.error(e.getLocalizedMessage(), e);
throw new RuntimeException(e.getLocalizedMessage(), e);
}
}
}
/**
* Collect rules based on tests and its context
*
* @param testMethod
* @param context
* @return rules list
*/
private List<Class<? extends IRule>> collectRules(Method testMethod, ITestContext context) {
List<Class<? extends IRule>> rules = new ArrayList<>();
// collect rules from current class and method
ExpectedSkip classSkipAnnotation = testMethod.getDeclaringClass().getAnnotation(ExpectedSkip.class);
ExpectedSkip methodSkipAnnotation = testMethod.getAnnotation(ExpectedSkip.class);
rules.addAll(getRulesFromAnnotation(classSkipAnnotation));
rules.addAll(getRulesFromAnnotation(methodSkipAnnotation));
// analyze all dependent methods and collect rules
ITestNGMethod[] methods = context.getAllTestMethods();
for (ITestNGMethod iTestNGMethod : methods) {
if (iTestNGMethod.getMethodName().equalsIgnoreCase(testMethod.getName())) {
String[] methodsDep = iTestNGMethod.getMethodsDependedUpon();
for (String method : methodsDep) {
rules.addAll(getDependentMethodsRules(method));
}
}
}
return rules;
}
@Override
public void onStart(final ITestContext context) {
final String parentUuid = getUniqueUuid(context.getSuite());
final String uuid = getUniqueUuid(context);
final TestResultContainer container = new TestResultContainer()
.setUuid(uuid)
.setName(context.getName())
.setStart(System.currentTimeMillis());
getLifecycle().startTestContainer(parentUuid, container);
Stream.of(context.getAllTestMethods())
.map(ITestNGMethod::getTestClass)
.distinct()
.forEach(this::onBeforeClass);
context.getExcludedMethods().stream()
.filter(ITestNGMethod::isTest)
.filter(method -> !method.getEnabled())
.forEach(method -> createFakeResult(context, method));
}
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
if (!isThisSuiteListener(testResult)) {
return;
}
if (method.isTestMethod()) {
ITestNGMethod thisMethod = method.getTestMethod();
ITestNGMethod[] allTestMethods = testResult.getTestContext().getAllTestMethods();
ITestNGMethod firstMethod = allTestMethods[0];
ITestNGMethod lastMethod = allTestMethods[allTestMethods.length - 1];
if (!thisMethod.equals(firstMethod) && !thisMethod.equals(lastMethod)) {
IResultMap success = testResult.getTestContext().getPassedTests();
if (!success.getAllMethods().contains(firstMethod)) {
throw new SkipException("Skipped because first method was not succcessfull");
}
}
}
}
@Override
public void afterInvocation(final IInvokedMethod method, final ITestResult testResult) {
final ITestNGMethod testMethod = method.getTestMethod();
if (isSupportedConfigurationFixture(testMethod)) {
final String executableUuid = currentExecutable.get();
currentExecutable.remove();
if (testResult.isSuccess()) {
getLifecycle().updateFixture(executableUuid, result -> result.setStatus(Status.PASSED));
} else {
getLifecycle().updateFixture(executableUuid, result -> result
.setStatus(getStatus(testResult.getThrowable()))
.setStatusDetails(getStatusDetails(testResult.getThrowable()).orElse(null)));
}
getLifecycle().stopFixture(executableUuid);
if (testMethod.isBeforeMethodConfiguration() || testMethod.isAfterMethodConfiguration()) {
final String containerUuid = currentTestContainer.get();
validateContainerExists(getQualifiedName(testMethod), containerUuid);
currentTestContainer.remove();
getLifecycle().stopTestContainer(containerUuid);
getLifecycle().writeTestContainer(containerUuid);
}
}
}
private List<Label> getLabels(final ITestNGMethod method, final IClass iClass) {
final List<Label> labels = new ArrayList<>();
getMethod(method)
.map(AnnotationUtils::getLabels)
.ifPresent(labels::addAll);
getClass(iClass)
.map(AnnotationUtils::getLabels)
.ifPresent(labels::addAll);
getMethod(method)
.map(this::getSeverity)
.filter(Optional::isPresent)
.orElse(getClass(iClass).flatMap(this::getSeverity))
.map(ResultsUtils::createSeverityLabel)
.ifPresent(labels::add);
return labels;
}
/**
* get Test Method name
*
* @param result ITestResult
* @return String method name
*/
public static String getMethodName(ITestResult result) {
// adjust testName using pattern
ITestNGMethod m = result.getMethod();
String name = Configuration.get(Configuration.Parameter.TEST_NAMING_PATTERN);
name = name.replace(SpecialKeywords.METHOD_NAME, m.getMethodName());
name = name.replace(SpecialKeywords.METHOD_PRIORITY, String.valueOf(m.getPriority()));
name = name.replace(SpecialKeywords.METHOD_THREAD_POOL_SIZE, String.valueOf(m.getThreadPoolSize()));
if (m.getDescription() != null) {
name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, m.getDescription());
} else {
name = name.replace(SpecialKeywords.METHOD_DESCRIPTION, "");
}
return name;
}
private String qualifiedName(ITestNGMethod method) {
StringBuilder addon = new StringBuilder();
String[] groups = method.getGroups();
int length = groups.length;
if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {
addon.append("(");
for (int i = 0; i < length; i++) {
if (i > 0) {
addon.append(", ");
}
addon.append(groups[i]);
}
addon.append(")");
}
return "<b>" + method.getMethodName() + "</b> " + addon;
}
private boolean isInActiveGroupWithIncludes(ITestNGMethod method, List<String> includeGroups, List<String> excludeGroups) {
if (method.getGroups() != null) {
boolean included = false;
boolean excluded = false;
for (String group : method.getGroups()) {
if (includeGroups.contains(group)) {
included = true;
}
if (excludeGroups.contains(group)) {
excluded = true;
}
}
if (included && !excluded) {
return true; // a group of the method is included and not excluded
}
}
return false; // no groups of the method are included
}
@SuppressWarnings("unchecked")
private static Tuple2<Class<?>, String> getFactoryAnnotationMetaData(final ITestNGMethod testMethod) {
var constructor = testMethod.getConstructorOrMethod().getConstructor();
var method = testMethod.getConstructorOrMethod().getMethod();
var factoryAnnotation = nonNull(method)
? ofNullable(method.getDeclaredAnnotation(Factory.class))
: ofNullable(constructor.getDeclaredAnnotation(Factory.class));
var dataProviderClass = factoryAnnotation
.map(fa -> (Class) fa.dataProviderClass())
.filter(cl -> cl != Object.class)
.orElseGet(() -> testMethod.getConstructorOrMethod().getDeclaringClass());
var dataProviderMethod = factoryAnnotation.map(Factory::dataProvider).orElse("");
return Tuple.of(dataProviderClass, dataProviderMethod);
}
/**
* Checks if the given test method is part of the current test run in matters of the include/exclude group filtering
*/
private boolean isInActiveGroup(ITestNGMethod method, ITestContext context) {
String[] includedGroupsArray = context.getIncludedGroups();
List<String> includeGroups = includedGroupsArray != null ? asList(includedGroupsArray) : Collections.<String>emptyList();
String[] excludedGroupsArray = context.getExcludedGroups();
List<String> excludeGroups = excludedGroupsArray != null ? asList(excludedGroupsArray) : Collections.<String>emptyList();
if (includeGroups.isEmpty()) {
if (excludeGroups.isEmpty()) {
return true; // no group restriction
} else {
return isInActiveGroupWithoutIncludes(method, excludeGroups);
}
} else {
return isInActiveGroupWithIncludes(method, includeGroups, excludeGroups);
}
}
/**
* Print out test summary
*/
private void logResults() {
//
// Log test summary
//
ITestNGMethod[] ft = resultsToMethods(getFailedTests());
StringBuilder sb = new StringBuilder("\n===============================================\n");
sb.append(" ").append(suiteName).append("\n");
sb.append(" Tests run: ").append(Utils.calculateInvokedMethodCount(getAllTestMethods()));
sb.append(", Failures: ").append(Utils.calculateInvokedMethodCount(ft));
sb.append(", Skips: ").append(Utils.calculateInvokedMethodCount(resultsToMethods(getSkippedTests())));
int confFailures = getConfigurationFailures().size();
int confSkips = getConfigurationSkips().size();
if (confFailures > 0 || confSkips > 0) {
sb.append("\n").append(" Configuration Failures: ").append(confFailures);
sb.append(", Skips: ").append(confSkips);
}
sb.append("\n===============================================");
log(sb.toString());
}
private static int getSkipCnt(ITestContext context) {
if ((context != null) && (context.getSkippedTests() != null)) {
if (context.getSkippedTests().getAllResults() != null) {
Collection<ITestNGMethod> skippedTest =
context.getSkippedTests().getAllMethods();
Set<ITestNGMethod> set = new HashSet<ITestNGMethod>(skippedTest);
if (ApplicationProperties.RETRY_CNT.getIntVal(0) > 0) {
set.removeAll(context.getPassedTests().getAllMethods());
set.removeAll(context.getFailedTests().getAllMethods());
return set.size();
}
return context.getSkippedTests().getAllResults().size();
}
return context.getSkippedTests().size();
}
return 0;
}
private static Iterator<Object[]> invokeCustomDataProvider(ITestNGMethod tm, ITestContext c, String dp,
String dpc) {
String methodClass = tm.getConstructorOrMethod().getDeclaringClass().getName();
if (isBlank(dpc)) {
dpc = getBundle().getString("global.dataproviderclass", getBundle().getString("dataproviderclass",methodClass));
}
if (isNotBlank(dpc)) {
Method m;
try {
m = getDataProviderMethod(dp, dpc);
} catch (Exception e) {
m = getDataProviderMethod(dp, methodClass);
}
Object instanceToUse = m.getDeclaringClass().equals(tm.getConstructorOrMethod().getDeclaringClass()) ? tm.getInstance()
: ClassHelper.newInstanceOrNull(m.getDeclaringClass());
return InvocatoinHelper.invokeDataProvider(instanceToUse, m, tm, c, null,
new Configuration().getAnnotationFinder());
} else {
throw new DataProviderException(
"Data-provider class not found. Please provide fully qualified class name as dataProviderClass");
}
}
@SuppressWarnings("unchecked")
private static boolean applyMetafilter(ITestNGMethod imethod, Map<String, Object> scenarioMetadata) {
String includeStr = getParameter(imethod, "include");
String excludeStr = getParameter(imethod, "exclude");
if (StringUtil.isBlank(includeStr) && StringUtil.isBlank(excludeStr)) {
// no need to process as no include/exclude filter provided
return true;
}
Gson gson = new GsonBuilder().create();
Map<String, Object> includeMeta = gson.fromJson(includeStr, Map.class);
Map<String, Object> excludeMeta = gson.fromJson(excludeStr, Map.class);
return MetaDataScanner.includeMethod(scenarioMetadata, includeMeta, excludeMeta);
}
protected ITestResult prepareMock(Class<?> tClass, Method method) {
ITestResult result = mock(ITestResult.class);
IClass clazz = mock(IClass.class);
ITestNGMethod testNGMethod = mock(ITestNGMethod.class);
ConstructorOrMethod cm = mock(ConstructorOrMethod.class);
String methodName = method.getName();
when(result.getTestClass()).thenReturn(clazz);
when(result.getTestClass().getRealClass()).thenReturn(tClass);
when(clazz.getName()).thenReturn(this.getClass().getName());
when(result.getMethod()).thenReturn(testNGMethod);
when(cm.getMethod()).thenReturn(method);
when(result.getMethod().getConstructorOrMethod()).thenReturn(cm);
when(testNGMethod.getMethodName()).thenReturn(methodName);
ITestContext context = mock(ITestContext.class);
when(result.getTestContext()).thenReturn(context);
XmlTest xmlTest = new XmlTest();
XmlSuite suite = new XmlSuite();
xmlTest.setXmlSuite(suite);
suite.setListeners(Arrays.asList(VideoListener.class.getName()));
when(context.getCurrentXmlTest()).thenReturn(xmlTest);
return result;
}
/**
* Skip test if preceding test with higher priority from the same test class has failed.
*
* @param result holds result of test execution
* @throws SkipException if test should be skipped
*/
private void skipTestIfNeeded(ITestResult result) {
ITestNGMethod testMethodToSkip = result.getMethod();
ITestResult failedTestResult =
testsWithFailure.get(testMethodToSkip.getInstance().getClass().getName());
// skip test with lower priority value and if it shouldn't always run
if (failedTestResult != null
&& testMethodToSkip.getPriority() > failedTestResult.getMethod().getPriority()
&& !testMethodToSkip.isAlwaysRun()) {
throw new SkipException(
format(
"Skipping test %s because it depends on test %s which has failed earlier.",
getStartingTestLabel(testMethodToSkip),
getCompletedTestLabel(failedTestResult.getMethod())));
}
}
public void onFinish(ITestContext iTestContext) {
Object id;
synchronized (lock) {
id = suites.remove(iTestContext.getName());
for (ITestNGMethod method : iTestContext.getAllTestMethods()) {
testMethodToSuiteMapping.remove(method);
}
}
resultProcessor.completed(id, new TestCompleteEvent(iTestContext.getEndDate().getTime()));
}
public void onStart(ITestContext iTestContext) {
TestDescriptorInternal testInternal;
synchronized (lock) {
testInternal = new DefaultTestSuiteDescriptor(idGenerator.generateId(), iTestContext.getName());
suites.put(testInternal.getName(), testInternal.getId());
for (ITestNGMethod method : iTestContext.getAllTestMethods()) {
testMethodToSuiteMapping.put(method, testInternal.getId());
}
}
resultProcessor.started(testInternal, new TestStartEvent(iTestContext.getStartDate().getTime()));
}
public void onFinish(ITestContext iTestContext) {
Object id;
synchronized (lock) {
id = suites.remove(iTestContext.getName());
for (ITestNGMethod method : iTestContext.getAllTestMethods()) {
testMethodToSuiteMapping.remove(method);
}
}
resultProcessor.completed(id, new TestCompleteEvent(iTestContext.getEndDate().getTime()));
}
@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context)
{
for ( ITestNGMethod method : context.getAllTestMethods() )
{
method.setRetryAnalyzer(new RetryTest());
}
}
/**
* Add the specified method to the method list
*
* @param methodList list of methods that are about to be run
* @param testMethod method to be added to the list
* @param platformConstant target platform on which to run this method
*/
@SuppressWarnings("unchecked")
private static <P extends Enum<?> & PlatformEnum> void addMethodForPlatform(List<IMethodInstance> methodList, IMethodInstance testMethod, PlatformEnum platformConstant) {
if (platformConstant != null) {
ITestNGMethod method = testMethod.getMethod();
PlatformIdentity<P> identity = new PlatformIdentity<>((P) platformConstant, method.getDescription());
testMethod.getMethod().setDescription(DataUtils.toString(identity));
}
methodList.add(testMethod);
}
@Override
public void onFinish(final ITestContext context) {
final String uuid = getUniqueUuid(context);
getLifecycle().stopTestContainer(uuid);
getLifecycle().writeTestContainer(uuid);
Stream.of(context.getAllTestMethods())
.map(ITestNGMethod::getTestClass)
.distinct()
.forEach(this::onAfterClass);
}
private void initRestResource(ITestNGMethod... testMethods) {
for (ITestNGMethod testMethod : testMethods) {
Object instance = testMethod.getInstance();
if (hasEverrestJettyListenerTestHierarchy(instance.getClass())) {
EverrestApplication everrest = new EverrestApplication();
Field[] fields = instance.getClass().getDeclaredFields();
for (Field field : fields) {
try {
if (isRestResource(field.getType())) {
ObjectFactory<? extends ObjectModel> factory = createFactory(instance, field);
if (factory != null) {
everrest.addFactory(factory);
}
} else if (javax.servlet.Filter.class.isAssignableFrom(field.getType())) {
field.setAccessible(true);
Object fieldInstance = field.get(instance);
if (fieldInstance != null) {
httpServer.addFilter(((javax.servlet.Filter)fieldInstance), "/*");
} else {
httpServer.addFilter((Class<? extends javax.servlet.Filter>)field.getType(), "/*");
}
}
} catch (IllegalAccessException e) {
LOG.error(e.getLocalizedMessage(), e);
}
}
if (everrest.getFactories().size() > 0) {
httpServer.publish(everrest);
}
}
}
}
@Override
public void beforeInvocation(final IInvokedMethod method, final ITestResult testResult) {
final ITestNGMethod testMethod = method.getTestMethod();
final ITestContext context = testResult.getTestContext();
if (isSupportedConfigurationFixture(testMethod)) {
ifSuiteFixtureStarted(context.getSuite(), testMethod);
ifTestFixtureStarted(context, testMethod);
ifClassFixtureStarted(testMethod);
ifMethodFixtureStarted(testMethod);
}
}
private void ifSuiteFixtureStarted(final ISuite suite, final ITestNGMethod testMethod) {
if (testMethod.isBeforeSuiteConfiguration()) {
startBefore(getUniqueUuid(suite), testMethod);
}
if (testMethod.isAfterSuiteConfiguration()) {
startAfter(getUniqueUuid(suite), testMethod);
}
}
private void ifClassFixtureStarted(final ITestNGMethod testMethod) {
if (testMethod.isBeforeClassConfiguration()) {
getClassContainer(testMethod.getTestClass())
.ifPresent(parentUuid -> startBefore(parentUuid, testMethod));
}
if (testMethod.isAfterClassConfiguration()) {
getClassContainer(testMethod.getTestClass())
.ifPresent(parentUuid -> startAfter(parentUuid, testMethod));
}
}
@Override
public boolean isPerform(ITestNGMethod testMethod, List<String> expectedData) {
TestPriority priority = testMethod.getConstructorOrMethod().getMethod().getAnnotation(TestPriority.class);
if (priority != null) {
Priority testPriority = priority.value();
String actualTestPriority = testPriority.toString();
LOGGER.info(String.format("Test: [%s]. Priority: [%s]. Expected priority: [%s]", testMethod.getMethodName(), actualTestPriority,
expectedData.toString()));
return expectedData.parallelStream().anyMatch(d -> d.equals(actualTestPriority));
}
return false;
}
@SuppressWarnings("deprecation")
private FixtureResult getFixtureResult(final ITestNGMethod method) {
final FixtureResult fixtureResult = new FixtureResult()
.withName(getMethodName(method))
.withStart(System.currentTimeMillis())
.withDescription(method.getDescription())
.withStage(Stage.RUNNING);
processDescription(getClass().getClassLoader(), method.getConstructorOrMethod().getMethod(), fixtureResult);
return fixtureResult;
}
protected String getHistoryId(final ITestNGMethod method, final List<Parameter> parameters) {
final MessageDigest digest = getMd5Digest();
final String testClassName = method.getTestClass().getName();
final String methodName = method.getMethodName();
digest.update(testClassName.getBytes(UTF_8));
digest.update(methodName.getBytes(UTF_8));
parameters.stream()
.sorted(comparing(Parameter::getName).thenComparing(Parameter::getValue))
.forEachOrdered(parameter -> {
digest.update(parameter.getName().getBytes(UTF_8));
digest.update(parameter.getValue().getBytes(UTF_8));
});
final byte[] bytes = digest.digest();
return bytesToHex(bytes);
}