Hello all welcome back to the next one. In this one we'll talk about reporters in test engine. And also how can we generate HTML reports using test engine. So reporters are nothing but kind of for replacement of logs for Jay. I mean, the log for j is a different tool or API's that we usually use to write logs, like whatever statements that we want to print. Usually we don't use this system dot out dot print ln, right?
We have different login mechanism in our application, and the most popular one is log for j or maybe lock for j two. So we can log statements in console or we can log statements in external files also. But test engine also provides one more mechanism to log their statements and that is known as reporter The only benefit is the state That we log using the reporter goes into the reports that are generated by test Angie automatically. So that's the only benefit I see. But I don't see it as a replacement of log for se because I would still recommend you guys to continue using whatever you're using. It completely depends on you what you want to use in your application.
I generally prefer lock for j two. But reporter is also a good stuff. Basically what you can do is you can do a mix and match of both things. For all individual actions, you can probably use log for J. And then for high level actions like test started, test, entered or maybe test finished or maybe some high level actions. You can use reporter so you can see them in test injury reports, if at all you are using custom reports.
So let's take a look. What I have here is I have a test engineer Under score reports and logs file created, it's just a usual file that we always create. It has methods like before class after class. Before method after method, and three test methods I created test method one, it's gonna be normal successful test case, test method two, I'm intentionally failing this test method and test method three. It is also a normal test case. But what happens is it depends on test method two.
So if test method two fails, then we're going to have test method three skip. I'm intentionally doing it so we can have three different kinds of results. One is successful one is fail and one escape so we can see all of them in the report. So till now we were just using System dot out dot print ln statements with reporter what we can do here is I'm going to first show you the import statement. So this is the import statement. Import or dot test nj dot reporter, make sure that you have the correct import statement.
Once you have that, then let me just let me just delete it and write it again for you. So we will say reporter dot log. And these are different overloads. I'm going to use the second one, and I'll show you what that is. So it accepts two arguments. The first one is string s, which is the message that we want to log.
And the second one is log to standard out. Which means if we want to print the same message on the console or not standard out is the console. So we can use the same message from here. And I'm just gonna use the same message and this manifests a false. Let's see the difference. I'm gonna just say false and right click This file, run it as test in G test.
And it ran the test case, we can see three tests run one failed one skip and one was successful we can see it here as well. And here it printed out once this runs before every class and then the, this runs before every method. So, before every class has printed only once, even though we have two statements here, right? Even though we have two statements it only printed once. The reason is, we said false to standard out if I provided true it's gone up printed twice first from the System dot out dot print ln and then second from the reporter dot log. So again, run it as test and the test.
And now we can see this runs once before class is printed twice. And we got it by right. So what we can do here We can actually replace the System dot out dot print ln statements with the reporter dot lot, so I'm just gonna maybe copy all of them. And we can use the second overload and everything. So just cut it from there, paste it. And this one can always be kept true, because we always want to log.
Currently at least for the demo purpose, we always want to log to the console so we can see the output really handy. If we don't print it here, we will not be able to see it in the console. So it's just a good idea to keep it there. So let's replace all these messages. I'm just gonna show you that. Even if we remove every System dot out dot print ln statement.
We are still good to go. We can achieve the same thing. Using the reporter logs, so let's just quickly try to replace them. And all these are done, only three methods are left. So let me just quickly copy them here so we can save some time. And copy these messages.
Delete System dot out dot print ln statement and copy this message. Delete this one. And we are just left with the last statement. So we'll just put it here. And that's about it. So now we're not using any of the System dot out dot print ln statements.
And we are just using reporter dot log and we can run As test in G test, and we can still see everything getting printed up here, because we are using the true as the argument. Now the other part of the question is generating the HTML reports. What do we have to do to generate the reports? The good part is we don't have to do anything. Tests engine takes care of it automatically. And it does it.
The only thing that we want to do here is we cannot generate reports. If we right click the file and run the code, we have to create an XML file that sn G Suite file, and then run that as a test in G Suite. And then it will automatically generate the report for us. So let me just do that. So I created this XML file. It's again the same file there is no difference.
I just change the class name. If I run this file, right click, run as test in G Suite, you will see the difference. So just pay attention on the left side in the project and you We'll see one test output folder. So it has run, everything is run successfully. And when I refresh the project, right click the project, refresh it. There we go, we see this test dash output folder.
And if I expand it, I can see a lot of different things here. So there are two reports. One is email, double dash report dot html, and the other is index dot HTML. We can open them from here as well. If I double click it, it will open in Eclipse. But since they are HTML files, we should actually go to the location.
And we should be able to open it on the browser as a normal file also. And if you don't know where these files are located on your system, just right click this one or maybe just right click your project and see the properties you will see the location and current windows also you will still be able to see the location. So you need to figure out where you created this one. So Have it on documents workspace personal. And there is the project that I created test engine tutorial. So what I'm gonna do here is I'm gonna do two documents, and my workspace personal.
And then test engine tutorial. This is the project. So everything is right here inside it, I can go to the source folder, I can figure out all the packages. Everything is there, I can copy any code file also from here. So here is the test output folder. I'm going to double click it, it's going to present me all the files.
So these are the two files email label report dot html. And this opens in Firefox by default or any other browser that you have. So this is the report kind of report we have. And the good part is now since we had three different kind of test cases, it shows us three different places. So this one failed test method and it's in red, the one which was skipped is grayed out test method three and the one which was successful is increasing test methods and we can see some error messages. Also for test method two, we can click on them to actually reach where these methods are.
So this one is for test method one and everything is successful, there is no other messages. So it's all good. And here in the summary, it shows the test name was application test, and it's coming from the XML file. And then how many are successful, how many has cabe and how many are fail and how much total time it took. So it's a pretty decent report and the one we opened was email label report dot html. And now let's quickly take a look at the index dot HTML.
So this one is a different format, but essentially gives us the same thing. So you can see three methods here one failed, one skipped one was successful. You can click on the test method to show you the errors. You can click on test method three, I'll show you what happens. With that, and so basically, it's already showing the name of the class as method two is a test method, three cells, everything is shown. And then what was successful when I clicked show so we can hide and show right?
When I click Show, it shows me for the test method one also, the other part here interesting is this report on output. So if I click on this, it tells me the output from the reporter. So test method one. This was the statement that be printed. From test method two, this was the statement that we printed, so anything that we put in using the reporter log, these statements are shown in the report up here. Yes, but we don't see anything from the before class before method and those things.
Because there isn't it it is only kind of concerned about the test methods. So we see the statements only from the testament. That's here. So there we can see the report output also. So pretty much kind of a decent report. It's not really fancy, but it's still a decent report to start with, you can kind of use it.
And that's about it. So the good part is we don't need to do anything extra. It's it just comes by default. It just comes with the default settings test and it does everything for us. So it's still quite handy. So that's about it.
I will say please go ahead, download the code file practice a little bit. And let me know if you have any questions. Thanks for attending this one. Thanks.