How to dynamically specify the package to @RunWith with JUnit4

不想你离开。 提交于 2020-01-05 07:05:27

问题


Thanks to my previous question, my framework now has a runner that will count @Category tags so I can get a clear picture of code coverage from my testing. My next step is to abstract the runner as the one framework handles multiple apps, divided into packages.

+---main
+---test
     +---java
         +---com
             +---company
                +---app
                ¦   +---app1
                ¦   ¦   +---common
                ¦   ¦   +---page
                ¦   ¦   +---test
                ¦   +---app2
                ¦       +---common
                ¦       +---page
                ¦       +---test
                +---core
                    +---categories

The framework uses PageObject Model so page will contain each page's details, common will be for features common across the whole app, and test holds the actual tests named *Test.class

Currently the runner is located in the same package as each app's tests. But each one is exactly the same code except for a variable holding the package path, and the @SuiteClasses() listing each test class.

@RunWith(Suite.class)
@Suite.SuiteClasses({
        FirstTest.class,
        SecondTest.class,
        ThirdTest.class,
        _CleanUpTest.class})
public class _CategoriesCount {
    private static final String MAIN_TEST_PACKAGES = "com.company.app.app1.test";
    private static final String MAIN_CATEGORY_PATH = "com.company.core.categories.";
    ...
}

What I'm hoping for is to create a single runner inside the core package and based on either command line, or a variable within the pom.xml, to specify the app package folder and the runner automatically configures itself. This would have a couple of advantages:

  • Dynamically read in the *Test.class files so if a new set of tests is created, the tester doesn't have to remember to add it to the runner file
  • There is only one runner; only one place to fix/enhance instead of dozens

Unfortunately, everything I've seen calls for the annotations to be static. I believe this is due to timing of when the runner is read prior to code execution. So I'm seeing if there is some way I'm not finding to add a -DtestApp=app2 to the command line and that in turn can fill in the runner. I already use the surefire plug-in to set other variables within the code, so I'm hoping I can do the same with annotations.

来源:https://stackoverflow.com/questions/53936561/how-to-dynamically-specify-the-package-to-runwith-with-junit4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!