[xwiki-devs] [VOTE] Unit tests must not output anything on the console
Hi devs, It's a best practice that unit tests must not output anything to the console. Rationale: * It pollutes the maven logs unnecessarily. For example imagine that you're expecting an error in your test and that the stacktrace is logged to the console. When looking at the maven console logs the reader will wonder if there was an error or if it was expected. * The test must assert whatever it needs to validate the code under test. So if it expects a log to be printed it should assert it. In order to make this easy I've just added JUnit LogRule to our commons test framework. Here's a usage example: public class RestrictParseLocationEventHandlerTest { private RestrictParseLocationEventHandler handler = new RestrictParseLocationEventHandler(); /** * Capture logs with WARN or higher severity to assert them. */ @Rule public LogRule logRule = new LogRule() {{ record(LogLevel.WARN); recordLoggingForType(RestrictParseLocationEventHandler.class); }}; @Test public void includeEventWhenIllegalPath() { Assert.assertNull("Template shouldn't have been returned", this.handler.includeEvent("../WEB-INF/xwiki.cfg", "xwiki:Main.WebHome", "parse")); Assert.assertEquals(1, this.logRule.size()); Assert.assertTrue(this.logRule.contains( "Direct access to template file [/WEB-INF/xwiki.cfg] refused. Possible break-in attempt!")); } } So this is a vote to agree on this best practice and to document it on dev.xwiki.org. Here's my +1 If you agree then please make sure your tests don't output anything from now on and please try fixing the tests you wrote that do... Thanks -Vincent
+1 On Thu, Sep 20, 2012 at 8:25 AM, Vincent Massol <vincent@massol.net> wrote:
Hi devs,
It's a best practice that unit tests must not output anything to the console. Rationale:
* It pollutes the maven logs unnecessarily. For example imagine that you're expecting an error in your test and that the stacktrace is logged to the console. When looking at the maven console logs the reader will wonder if there was an error or if it was expected. * The test must assert whatever it needs to validate the code under test. So if it expects a log to be printed it should assert it.
In order to make this easy I've just added JUnit LogRule to our commons test framework.
Here's a usage example:
public class RestrictParseLocationEventHandlerTest { private RestrictParseLocationEventHandler handler = new RestrictParseLocationEventHandler();
/** * Capture logs with WARN or higher severity to assert them. */ @Rule public LogRule logRule = new LogRule() {{ record(LogLevel.WARN); recordLoggingForType(RestrictParseLocationEventHandler.class); }};
@Test public void includeEventWhenIllegalPath() { Assert.assertNull("Template shouldn't have been returned", this.handler.includeEvent("../WEB-INF/xwiki.cfg", "xwiki:Main.WebHome", "parse"));
Assert.assertEquals(1, this.logRule.size()); Assert.assertTrue(this.logRule.contains( "Direct access to template file [/WEB-INF/xwiki.cfg] refused. Possible break-in attempt!")); } }
So this is a vote to agree on this best practice and to document it on dev.xwiki.org.
Here's my +1
If you agree then please make sure your tests don't output anything from now on and please try fixing the tests you wrote that do...
Thanks -Vincent
_______________________________________________ devs mailing list devs@xwiki.org http://lists.xwiki.org/mailman/listinfo/devs
-- Thomas Mortagne
+1 On Thu, Sep 20, 2012 at 8:25 AM, Vincent Massol <vincent@massol.net> wrote:
Hi devs,
It's a best practice that unit tests must not output anything to the console. Rationale:
* It pollutes the maven logs unnecessarily. For example imagine that you're expecting an error in your test and that the stacktrace is logged to the console. When looking at the maven console logs the reader will wonder if there was an error or if it was expected. * The test must assert whatever it needs to validate the code under test. So if it expects a log to be printed it should assert it.
In order to make this easy I've just added JUnit LogRule to our commons test framework.
Here's a usage example:
public class RestrictParseLocationEventHandlerTest { private RestrictParseLocationEventHandler handler = new RestrictParseLocationEventHandler();
/** * Capture logs with WARN or higher severity to assert them. */ @Rule public LogRule logRule = new LogRule() {{ record(LogLevel.WARN); recordLoggingForType(RestrictParseLocationEventHandler.class); }};
@Test public void includeEventWhenIllegalPath() { Assert.assertNull("Template shouldn't have been returned", this.handler.includeEvent("../WEB-INF/xwiki.cfg", "xwiki:Main.WebHome", "parse"));
Assert.assertEquals(1, this.logRule.size()); Assert.assertTrue(this.logRule.contains( "Direct access to template file [/WEB-INF/xwiki.cfg] refused. Possible break-in attempt!")); } }
So this is a vote to agree on this best practice and to document it on dev.xwiki.org.
Here's my +1
If you agree then please make sure your tests don't output anything from now on and please try fixing the tests you wrote that do...
Thanks -Vincent
_______________________________________________ devs mailing list devs@xwiki.org http://lists.xwiki.org/mailman/listinfo/devs
-- Denis Gervalle SOFTEC sa - CEO eGuilde sarl - CTO
+1 Thanks, Marius On Thu, Sep 20, 2012 at 9:25 AM, Vincent Massol <vincent@massol.net> wrote:
Hi devs,
It's a best practice that unit tests must not output anything to the console. Rationale:
* It pollutes the maven logs unnecessarily. For example imagine that you're expecting an error in your test and that the stacktrace is logged to the console. When looking at the maven console logs the reader will wonder if there was an error or if it was expected. * The test must assert whatever it needs to validate the code under test. So if it expects a log to be printed it should assert it.
In order to make this easy I've just added JUnit LogRule to our commons test framework.
Here's a usage example:
public class RestrictParseLocationEventHandlerTest { private RestrictParseLocationEventHandler handler = new RestrictParseLocationEventHandler();
/** * Capture logs with WARN or higher severity to assert them. */ @Rule public LogRule logRule = new LogRule() {{ record(LogLevel.WARN); recordLoggingForType(RestrictParseLocationEventHandler.class); }};
@Test public void includeEventWhenIllegalPath() { Assert.assertNull("Template shouldn't have been returned", this.handler.includeEvent("../WEB-INF/xwiki.cfg", "xwiki:Main.WebHome", "parse"));
Assert.assertEquals(1, this.logRule.size()); Assert.assertTrue(this.logRule.contains( "Direct access to template file [/WEB-INF/xwiki.cfg] refused. Possible break-in attempt!")); } }
So this is a vote to agree on this best practice and to document it on dev.xwiki.org.
Here's my +1
If you agree then please make sure your tests don't output anything from now on and please try fixing the tests you wrote that do...
Thanks -Vincent
_______________________________________________ devs mailing list devs@xwiki.org http://lists.xwiki.org/mailman/listinfo/devs
+1 Thanks, Eduard On Thu, Sep 20, 2012 at 4:54 PM, Marius Dumitru Florea < mariusdumitru.florea@xwiki.com> wrote:
+1
Thanks, Marius
On Thu, Sep 20, 2012 at 9:25 AM, Vincent Massol <vincent@massol.net> wrote:
Hi devs,
It's a best practice that unit tests must not output anything to the console. Rationale:
* It pollutes the maven logs unnecessarily. For example imagine that you're expecting an error in your test and that the stacktrace is logged to the console. When looking at the maven console logs the reader will wonder if there was an error or if it was expected. * The test must assert whatever it needs to validate the code under test. So if it expects a log to be printed it should assert it.
In order to make this easy I've just added JUnit LogRule to our commons test framework.
Here's a usage example:
public class RestrictParseLocationEventHandlerTest { private RestrictParseLocationEventHandler handler = new RestrictParseLocationEventHandler();
/** * Capture logs with WARN or higher severity to assert them. */ @Rule public LogRule logRule = new LogRule() {{ record(LogLevel.WARN); recordLoggingForType(RestrictParseLocationEventHandler.class); }};
@Test public void includeEventWhenIllegalPath() { Assert.assertNull("Template shouldn't have been returned", this.handler.includeEvent("../WEB-INF/xwiki.cfg", "xwiki:Main.WebHome", "parse"));
Assert.assertEquals(1, this.logRule.size()); Assert.assertTrue(this.logRule.contains( "Direct access to template file [/WEB-INF/xwiki.cfg] refused. Possible break-in attempt!")); } }
So this is a vote to agree on this best practice and to document it on dev.xwiki.org.
Here's my +1
If you agree then please make sure your tests don't output anything from now on and please try fixing the tests you wrote that do...
Thanks -Vincent
_______________________________________________ devs mailing list devs@xwiki.org http://lists.xwiki.org/mailman/listinfo/devs
devs mailing list devs@xwiki.org http://lists.xwiki.org/mailman/listinfo/devs
participants (5)
-
Denis Gervalle -
Eduard Moraru -
Marius Dumitru Florea -
Thomas Mortagne -
Vincent Massol