The other day a discussion arose on the TestNG users list about enhancing TestNG's dependency and group capabilities with more fine grained control of when tests get skipped, an idea I came up with which doesn't require any patches to TestNG was to make use of TestNG's Annotation Transformers coupled with an expression language such as mvel.
A bit of hacking tonight yields a working requirements transformer:
@Test
@Requires({"sys['user.name'] == 'amrk'"})
public void someTestThatOnlyRunsForMyUser() { ... }
When TestNG is invokved with the RequirementsTransformer each test that has an associated @Requires annotation is first vetted for validity using the mvel expression language. Currently the expressions context contains the current system properties in the variable 'sys' but the suites properties could be easily pulled in as well.
When an expression fails, the test is set to depend on a group with a name matching the expression:
java.lang.Throwable: Method test.TestWithRequirements.labla() depends on nonexistent group "sys['user.name'] != 'amrk'"
(currently this is the only way I can see to have an annotation transformer trigger a test skip).
This could be an easy to automatically filter tests from running on different hosts, environments, users - or it could just be overkill. Either way, the codes available for download for anyone to play with - I might throw this up into google-code as well thou.
Technorati Tags: development, java, testing, testng