简体   繁体   中英

Showing setup log info from the @BeforeTest annotation in a TestNG test

I'm developing some Selenium WebDriver tests in Java with testNg. I have a base class that launches the browser, navigates to a login page and logs me in. The method that does this in the base class is annotated with @BeforeTest . All my test classes extend this base class.

I've included some Reporter.log("...") calls in the @BeforeTest annotated method to log that the initial login was successful. However none of this output is appearing in the output files.

My question is: How can I include @BeforeTest annotated method's Reporter.log() output in the output file?

The only way I can think of doing it is to generate a log String of information in your dataprovider (during setup) , and then pass the string of log info to the @Test method using the Object[] parameter (optional TestNG method to the @BeforeTest and/or @Test methods). Then, your @BeforeTest method has access to that string and can print it out using Reporter.log.

So, in other words, you wont be able to get any @Configuration methods to show output on a test (as designed I think) BUT anything you do within a DataProvider could be passed forward to the @Test method and/or @Before configuration methods.

Another way you could do it is to keep a String variable in the test class that keeps log info in it. Then, during setup, save setup info to that string and then during the @Test method, access it and print it to the test output using Reporter.log(string, true); .

This just amounts to a circumstantial workaround that I would say you shouldn't do. If you really think you need to show setup information in test log output, I would recommend really thinking about WHY you think you need to do that since it probably means your doing something unusual and not common practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM