All about TestRule — A Steroid Before/After
Published in
5 min readAug 22, 2018
We usually have @Before
and @After
in our test for setup and teardown functions needed for the test. So what is testRule
?
It’s so commonly used
We have have seen it and used it, if one has use Espresso test before, and one very common way of using it is
But what is it? It looks magical.
The definition of TestRule
If you dive into the TestRule
interface code, you’ll see it as below
A TestRule is an alteration in how a test method, or set of test methods, is run and reported. A TestRule may add additional checks that cause a test that would otherwise fail to pass, or it may perform necessary setup or cleanup for tests, or it may observe test execution to report it elsewhere. TestRules can do everything that could be done previously with methods annotated with @Before, @After, @BeforeClass, or @AfterClass, but they are more powerful, and more easily shared between projects and classes.