private List<Parameter> getParameters(final ITestResult testResult) { final Stream<Parameter> tagsParameters = testResult.getTestContext() .getCurrentXmlTest().getAllParameters().entrySet() .stream() .map(entry -> new Parameter().withName(entry.getKey()).withValue(entry.getValue())); final String[] parameterNames = Optional.of(testResult) .map(ITestResult::getMethod) .map(ITestNGMethod::getConstructorOrMethod) .map(ConstructorOrMethod::getMethod) .map(Executable::getParameters) .map(Stream::of) .orElseGet(Stream::empty) .map(java.lang.reflect.Parameter::getName) .toArray(String[]::new); final String[] parameterValues = Stream.of(testResult.getParameters()) .map(this::convertParameterValueToString) .toArray(String[]::new); final Stream<Parameter> methodParameters = range(0, min(parameterNames.length, parameterValues.length)) .mapToObj(i -> new Parameter().withName(parameterNames[i]).withValue(parameterValues[i])); return Stream.concat(tagsParameters, methodParameters) .collect(Collectors.toList()); }
/** * Create the test methods that belong to this class (rejects * all those that belong to a different class). */ private ITestNGMethod[] createTestMethods(ITestNGMethod[] methods) { List<ITestNGMethod> vResult = Lists.newArrayList(); for (ITestNGMethod tm : methods) { ConstructorOrMethod m = tm.getConstructorOrMethod(); if (m.getDeclaringClass().isAssignableFrom(m_testClass)) { for (Object o : m_iClass.getInstances(false)) { log(4, "Adding method " + tm + " on TestClass " + m_testClass); vResult.add(new TestNGScenario(/* tm.getRealClass(), */ m.getMethod(), m_annotationFinder, m_xmlTest, o)); } } else { log(4, "Rejecting method " + tm + " for TestClass " + m_testClass); } } ITestNGMethod[] result = vResult.toArray(new ITestNGMethod[vResult.size()]); return result; }
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; }
private Map<String, ArrayList<String>> processTestContext( ITestContext context) { Map<String, ArrayList<String>> classMap = new HashMap<String, ArrayList<String>>(); Collection<ITestNGMethod> testMethods = Arrays.asList(context.getAllTestMethods()); for (ITestNGMethod testMethod : testMethods) { ConstructorOrMethod consMethod = testMethod.getConstructorOrMethod(); String methodName = consMethod.getName(); String className = consMethod.getDeclaringClass().getName(); ArrayList<String> methodList; if (!classMap.containsKey(className)) { methodList = new ArrayList<String>(); } else { methodList = classMap.get(className); } methodList.add(methodName); classMap.put(className, methodList); } return classMap; }
@Override public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { for (IMethodInstance methodIns : methods) { ITestNGMethod method = methodIns.getMethod(); ConstructorOrMethod meth = method.getConstructorOrMethod(); Method m = meth.getMethod(); if (m != null) { DB db = m.getAnnotation(DB.class); if (db != null) { TransactionLegacy txn = TransactionLegacy.open(m.getName()); } } } // TODO Auto-generated method stub return methods; }
@Before public void setUp() { testngListener = spy(new AllureTestListener()); allure = mock(Allure.class); testngListener.setLifecycle(allure); ISuite suite = mock(ISuite.class); when(suite.getName()).thenReturn(DEFAULT_SUITE_NAME); XmlTest xmlTest = mock(XmlTest.class); when(xmlTest.getName()).thenReturn(DEFAULT_XML_TEST_NAME); testContext = mock(ITestContext.class); when(testContext.getSuite()).thenReturn(suite); when(testContext.getCurrentXmlTest()).thenReturn(xmlTest); // mocking test method parameters ConstructorOrMethod constructorOrMethod = mock(ConstructorOrMethod.class); when(constructorOrMethod.getMethod()).thenReturn(parametrizedTestMethod(0, null, null, null)); method = mock(ITestNGMethod.class); when(method.getConstructorOrMethod()).thenReturn(constructorOrMethod); testResult = mock(ITestResult.class); when(testResult.getMethod()).thenReturn(method); when(testResult.getParameters()).thenReturn(new Object[]{}); IClass iClass = mock(IClass.class); when(testResult.getTestClass()).thenReturn(iClass); }
private <T extends Annotation> List<T> getAnnotationsOnMethod(final ITestResult result, final Class<T> clazz) { return Stream.of(result) .map(ITestResult::getMethod) .filter(Objects::nonNull) .map(ITestNGMethod::getConstructorOrMethod) .map(ConstructorOrMethod::getMethod) .flatMap(method -> Stream.of(method.getAnnotationsByType(clazz))) .collect(Collectors.toList()); }
private void deployResult(ITestResult tr, ITestContext context) { QAFTestBase stb = TestBaseProvider.instance().get(); try { if ((tr.getMethod() instanceof TestNGScenario) && ((tr.getStatus() == ITestResult.FAILURE) || (tr.getStatus() == ITestResult.SUCCESS || tr.getStatus() == ITestResult.SKIP))) { ConstructorOrMethod testCase = tr.getMethod().getConstructorOrMethod(); testCase.getMethod().getAnnotation(Test.class); TestCaseRunResult result = tr.getStatus() == ITestResult.SUCCESS ? TestCaseRunResult.PASS : tr.getStatus() == ITestResult.FAILURE ? TestCaseRunResult.FAIL : TestCaseRunResult.SKIPPED; // String method = testCase.getName(); String updator = getBundle().getString("result.updator"); if (StringUtil.isNotBlank(updator)) { Class<?> updatorCls = Class.forName(updator); TestCaseResultUpdator updatorObj = (TestCaseResultUpdator) updatorCls.newInstance(); TestNGScenario scenario = (TestNGScenario) tr.getMethod(); Map<String, Object> params = new HashMap<String, Object>(scenario.getMetaData()); params.put("duration", tr.getEndMillis() - tr.getStartMillis()); ResultUpdator.updateResult(result, stb.getHTMLFormattedLog() + stb.getAssertionsLog(), updatorObj, params); } } } catch (Exception e) { logger.warn("Unable to deploy result", e); } }
/** * Returns method annotation by specified annotation class from * from TestNG Method or null if the method does not contain * such annotation. * * @param annotation Annotation class to find * @param testResult Where to find * @return {@link Annotation} or null if doesn't exists */ private <T extends Annotation> T getMethodAnnotation(Class<T> annotation, ITestResult testResult) { ITestNGMethod testNGMethod = testResult.getMethod(); if (null != testNGMethod) { ConstructorOrMethod constructorOrMethod = testNGMethod.getConstructorOrMethod(); if (null != constructorOrMethod) { Method method = constructorOrMethod.getMethod(); if (null != method) { return method.getAnnotation(annotation); } } } return null; }
private String getMessageSenderNameForMethod(ITestNGMethod testMethod) { ConstructorOrMethod consMethod = testMethod.getConstructorOrMethod(); String methodName = consMethod.getName(); String className = consMethod.getDeclaringClass().getName(); String methodKey = methodName + "(" + className + ")"; return methodKey; }
@Override public ConstructorOrMethod getConstructorOrMethod() { return delegate.getConstructorOrMethod(); }
@SuppressWarnings( { "rawtypes", "unchecked" } ) public void onFinish( ITestContext context ) { LOGGER.info( context.getCurrentXmlTest().toXml( " " ) ); /** * IResultMap results = context.getPassedTests(); Set<ITestResult> * testResults = results.getAllResults(); for(ITestResult tResult : * testResults) { //tResult. } */ for ( ITestNGMethod method : context.getAllTestMethods() ) { ConstructorOrMethod mOrC = method.getConstructorOrMethod(); if ( mOrC == null ) { LOGGER.info( "ConstructorOrMethod null" ); continue; } Method m = mOrC.getMethod(); if ( m == null ) { LOGGER.info( "Method null" ); continue; } Class c = m.getDeclaringClass(); if ( c == null ) { LOGGER.info( "Class null" ); continue; } LOGGER.info( c.getCanonicalName() + ":" + m.getName() ); CatsTestStep catsTestStepAnnotation = m.getAnnotation( CatsTestStep.class ); CatsTestCase catsTestCaseAnnotation = ( CatsTestCase ) c.getAnnotation( CatsTestCase.class ); if ( catsTestStepAnnotation == null || catsTestCaseAnnotation == null ) { LOGGER.info( "Annotations not found" ); continue; } LOGGER.info( catsTestCaseAnnotation.name() + ":" + catsTestStepAnnotation.name() ); } LOGGER.info( "On Finish Found" ); }