Open kazurayam opened 5 months ago
I will read the source code of TestNG.
Possiblly this:
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
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
Reporter
class implements a static method getCurrentTestResult
with which I can get ITestObjectSo 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());
}
tests.LoginTest has the following code fragment:
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.