Can someone tell me please: how to take a screenshot when test method fails (jUnit 5). I have a base test class with BeforeEach
and AfterEach
methods. Any other classes with @Test methods extends base class.

- 17,493
- 11
- 81
- 103

- 31
- 2
-
Why do you want to take screenshots? And of what exactly? – Nicolai Parlog Nov 07 '16 at 10:57
2 Answers
Well, it is possible to write java code that takes screenshots, see here for example.
But I am very much wondering about the real problem you are trying to solve this way. I am not sure if you figured that yet, but the main intention of JUnit is to provide you a framework that runs your tests in various environments.
Of course it is nice that you can run JUnit within your IDE, and maybe you would find it helpful to get a screenshot. But: "normally" unit tests also run during nightly builds and such - in environments where "taking a screenshot" might not make any sense!
Beyond that: screenshorts are an extremely ineffective way of collecting information! When you have a fail, you should be locking for textual log files, html/xml reports, whatever. You want that failing tests generate information that can be easily digested.
So, the real answer here is: step back from what you are doing right now, and re-consider non-screenshot solutions to the problem you actually want to solve!
-
Hi! Thanks for your answer! But i'm using this framework with appium and running tests on ios real devices. It would be usefull to make screenshots to see, what tha problem was. – I.Vol Nov 08 '16 at 11:52
-
4When doing E2E tests with for example JUnit + Selenium it IS very useful to have both textual information & a screenshot. – Zoette Aug 17 '21 at 02:46
You don't need to take screen shots for JUnit test failes/passes, rather the recommended way is to generate various reports (Tests Passed/Failed Report, Code coverage Report, Code complexity Report etc..) automatically using the below tools/plugins.
You can use Cobertura maven plugin or Sonarqube code quality tool so that these will automatically generate the reports for you.
You can look here for Cobertura-maven-plugin and here for Sonarqube for more details.
You need to integrate these tools with your CI (Continuous Integration) environments and ensure that if the code is NOT passing certain quality (in terms of tests coverage, code complexity, etc..) then the project build (war/ear) should fail automatically.

- 21,832
- 11
- 51
- 67