kazurayam / ExtentReportsExample

ExtentReports 5.0.8 Example with TestNG Listeners and Retry Analyzer
https://www.swtestacademy.com/extent-reports-in-selenium-with-testng/
0 stars 0 forks source link

Question: how is TestNG configured to pass a Method object into @Test-annotated method invokation #5

Open kazurayam opened 5 months ago

kazurayam commented 5 months ago

tests.LoginTest has the following code fragment:

...
public class LoginTests extends BaseTest {
    @Test(priority = 0, description = "Invalid Login Scenario with wrong username and password.")
    public void invalidLoginTest_InvalidUserNameInvalidPassword(Method method) {
        //ExtentReports Description
        startTest(method.getName(), "Invalid Login Scenario with invalid username and password.");
        ...

I noticed that the @Test-annotated method could accept a parameter Method method. TestNG must be configured to pass the Method object when it invoke the @Test-annotated method. The parameterized Method object is very helpful.

How is it possible? I do not see how TestNG is configured to do that.

kazurayam commented 5 months ago

related article

https://stackoverflow.com/questions/49232177/how-to-use-parameters-together-with-itestcontext-in-testng

kazurayam commented 5 months ago

I will read the source code of TestNG.

Possiblly this:

https://github.com/testng-team/testng/blob/master/testng-core/src/main/java/org/testng/internal/invokers/MethodRunner.java

kazurayam commented 5 months ago

I found a document page. They call the idea Native Depency Injection.

https://testng-docs.readthedocs.io/fulldoc/#native-dependency-injection


Dependency injection TestNG supports two different kinds of dependency injection: native (performed by TestNG itself) and external (performed by a dependency injection framework such as Guice).

Native dependency injection TestNG lets you declare additional parameters in your methods. When this happens, TestNG will automatically fill these parameters with the right value.

Dependency injection can be used in the following places:

Any @Before method or @Test method can declare a parameter of type ITestContext. Any @AfterMethod method can declare a parameter of type ITestResult, which will reflect the result of the test method that was just run. Any @Before and @After methods (except @BeforeSuite and @AfterSuite) can declare a parameter of type XmlTest, which contain the current tag. Any @BeforeMethod (and @AfterMethod) can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this @BeforeMethod finishes (or after the method as run for @AfterMethod). Any @BeforeMethod can declare a parameter of type Object[]. This parameter will receive the list of parameters that are about to be fed to the upcoming test method, which could be either injected by TestNG, such as java.lang.reflect.Method or come from a @DataProvider. Any @DataProvider can declare a parameter of type ITestContext or java.lang.reflect.Method. The latter parameter will receive the test method that is about to be invoked.

kazurayam commented 5 months ago

I found a very interesting issue in the TestNG Github project

@Test annotated methods cannot inject java.lang.reflect.Method #1649

This issue told me that

So I do not have to write a @Test-annotated method to have Method or ITestContext as method parameters.

Here's a sample that shows how it can be retrieved.

    @Test
    public void anotherTestMethod() {
        ITestResult result = Reporter.getCurrentTestResult();
        System.err.println("Running " + result.getMethod().getConstructorOrMethod().getMethod().getName());
    }