}; ITestNGMethod[][] methods = new ITestNGMethod[][] { cls.getTestMethods(), cls.getBeforeClassMethods(), cls.getBeforeTestMethods(),
private String generateClass(ITestClass cls) { StringBuilder sb = new StringBuilder(); sb.append("<tr>\n") .append("<td>") .append(cls.getRealClass().getName()) .append("</td>\n") .append("<td> </td>") .append("<td> </td>") .append("</tr>\n"); String[] tags = new String[] {"@Test", "@BeforeClass", "@BeforeMethod", "@AfterMethod", "@AfterClass"}; ITestNGMethod[][] methods = new ITestNGMethod[][] { cls.getTestMethods(), cls.getBeforeClassMethods(), cls.getBeforeTestMethods(), cls.getAfterTestMethods(), cls.getAfterClassMethods() }; for (int i = 0; i < tags.length; i++) { sb.append("<tr>\n") .append("<td align='center' colspan='3'>") .append(tags[i]) .append("</td>\n") .append("</tr>\n") .append(dumpMethods(methods[i])); } return sb.toString(); }
@Test(description = "Test default methods defined in an interface should be run") public void testDefaultShouldRun() { ITestClass testClass = runTestWithDefaultMethods(); ITestNGMethod[] testMethods = testClass.getTestMethods(); Assert.assertEquals(testMethods.length, 1); Assert.assertEquals(testMethods[0].getMethodName(), "defaultMethodTest"); }
fixMethodsWithClass(tc.getTestMethods(), tc, testMethods); fixMethodsWithClass(tc.getBeforeClassMethods(), tc, beforeClassMethods); fixMethodsWithClass(tc.getBeforeTestMethods(), tc, null);
fixMethodsWithClass(tc.getTestMethods(), tc, testMethods); fixMethodsWithClass(tc.getBeforeClassMethods(), tc, beforeClassMethods); fixMethodsWithClass(tc.getBeforeTestMethods(), tc, null);
/** * NOTE: This callback is invoked once per method, so we count the methods per class to * fire our internal event only once. */ @Override public void onBeforeClass(ITestClass testClass, IMethodInstance mi) { if (numMethodsInvoked == 0) { numMethodsPerClass = testClass.getTestMethods().length; onceBeforeClass(testClass, mi); } }
private boolean isPriorityUnique(IInvokedMethod method) { // Logic to check priorities of all test methods are unique. This is used in Session Sharing. Set<Integer> check = new HashSet<Integer>(); int length = method.getTestMethod().getTestClass().getTestMethods().length; int expectedSize = 0; for (int i = 0; i < length; i++) { //ignore @Test(enabled = false) methods if (!method.getTestMethod().getTestClass().getTestMethods()[i].getEnabled()) { continue; } check.add(method.getTestMethod().getTestClass().getTestMethods()[i].getPriority()); expectedSize += 1; if (check.size() != expectedSize) { return false; } } return true; }
private boolean isLowPriority(IInvokedMethod method) { int low = method.getTestMethod().getPriority(); for (ITestNGMethod test : method.getTestMethod().getTestClass().getTestMethods()) { // ensures all test methods have the @Test annotation. Throw exception if that's not the case if (!isAnnotatedWithTest(test.getConstructorOrMethod().getMethod())) { throw new IllegalStateException( "Session sharing requires all test methods to define a priority via the @Test annotation."); } if (test.getEnabled() && test.getPriority() < low) { return false; } } Test t = method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class); // If there is an existing session and the test method has a DP then don't create a session // For a data driven test method with the first data the session must be created // Hence return true if currentInvocationCount is 1 otherwise utilize the same session // by returning false int currentInvocationCount = method.getTestMethod().getCurrentInvocationCount(); if (!t.dataProvider().isEmpty()) { return currentInvocationCount == 0; } return true; }
private boolean isHighPriority(IInvokedMethod method) { if (!isAnnotatedWithTest(method)) { // Abort. There will already be an exception thrown in isLowPriority for this case. return true; } int high = method.getTestMethod().getPriority(); for (ITestNGMethod test : method.getTestMethod().getTestClass().getTestMethods()) { if (test.getEnabled() && test.getPriority() > high) { return false; } } Test t = method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class); // For a test method with a data provider if (!(t.dataProvider().isEmpty())) { int currentInvocationCount = method.getTestMethod().getCurrentInvocationCount(); int parameterInvocationCount = method.getTestMethod().getParameterInvocationCount(); // If the data set from the data provider is exhausted // It means its the last method with the data provider- this is the exit condition return (currentInvocationCount == parameterInvocationCount); } return true; }