Java – can I specify a class wide group on a TestNG test case?
I have a base class to represent database tests in TestNG. I want to specify that all classes extended from this class are "DB test" group, but I find this seems infeasible I tried @ test note:
@Test(groups = { "db-test" }) public class DBTestBase { }
However, this does not work because the @ test annotation will try to put a bunch of methods into the test, and warnings / errors will pop up in eclipse when the test is run
So I try to disable testing, so at least these groups are assigned:
@Test(enabled = false,groups = { "db-test" }) public class DBTestBase { }
But then any @ beforetest (and other similar comments) is disabled... Of course it's not what I want
I want some methods to annotate classes as groups of specific types, but it seems unlikely in TestNG Does anyone have any other ideas?
Solution
The answer is through a custom org testng. IMethodSelector:
Its includemethod () can exclude any method we want, like a public not annotated method
However, to register a custom Java methodselector, you must add it to any xmltest instance managed by testrunner, which means you need your own custom testrunner
However, to build a custom testrunner, you need to register the testrunner factory with the - testrunfactory option
However, TestNG class will never be considered, so you also need to define a custom TestNG class:
>In order to override the configure (map) method, > so you can set testrunnerfactory. Testrunnerfactory will create a custom testrunner for you. Testrunner will set a custom xmlmethodselector > xmlmethodselector for the xmltest instance, which will build a self-defined imethodselector > imethodselector will exclude any TestNG method you choose!
Well, it's a nightmare But it is also a code challenge, so it must be a little challenging;)
All codes can be found in dzone snippets
Code challenges as usual:
>A Java class (and many internal classes) > paste the class into the "source / test" directory (because the package is' test ') > run it (no parameters are required)
Update for Mike stone:
I will accept this because it sounds very close to what I finally did, but I think I will add everything I did
Basically, I created a groups annotation that behaves like the groups attribute of the test (and other) annotation
Then, I created a groupsannotationtransformer, which uses iannotationtransformer to view all tests and test classes being defined, and then modify the tests to add groups, which is exactly consistent with group exclusion and inclusion
Modify the build to use the new annotation transformer, and it all works perfectly!
Well... A warning is that it won't add groups to non test methods... Because at this time, there is also an annotation transformer that allows you to convert anything, but it's not included in TestNG. I'm using it for some reason... So it's a good idea to make your pre / post annotation method alwaysrun = true... That's enough for me
The end result is what I can do:
@Groups({ "myGroup1","myGroup2"}) public class MyTestCase { @Test @Groups("aMethodLevelGroup") public void mytest() { } }
I make transformers work with subclasses and everything