Force run of @Ignore'd JUnit test using Maven command line

余生长醉 提交于 2019-12-30 02:56:08

问题


I have a JUnit test with an @Ignore attribute, for example -

public class HealthCareGov
{
    @Test
    @Ignore
    public void performancetest()
    {
        // stuff
    }
}

In most IDEs (IntelliJ for example) I can forcefully run this test by selecting the method name even though there is an @Ignore attribute. I'm guessing Eclipse also allows this?

However, when I try to do the same behavior in Maven by executing the following -

mvn -Dtest=HealthCareGov#performancetest test

It appears that Maven skips the test. This is the output -

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running HealthCareGov
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 1

What Maven command line can I use to force the execution of this test to reproduce the behavior that my IDE produces? Modifying the Java source code is not an option.


回答1:


This can be added to the test.

@IfProfileValue(name="test-profile", value="IntegrationTest")
public class PendingChangesITCase extends AbstractControllerIntegrationTest {
    ...
}

To select the tests to be executed just add the value to the profile for executing the integration tests.

<properties>
    <test-profile>IntegrationTest</test-profile>
</properties>

Also, you can use the command line to set the test-profile variable:

mvn -Dtest-profile=IntegrationTest



回答2:


Instead of overriding the @Ignore annotation, I did the following:

  • Name your test that you wish to only run from the command-line outside of the standard maven test runner nomenclature (surefire or failsafe). This will ensure that your test does not get picked up by the regular maven lifecycle test execution.
  • Write your test as normal, to include annotations for your framework on test methods.
  • When you wish to run your test, just include it manually by feeding it as a parameter to the runner.

    -Dtest=Foo # for surefire

    -Dit.test=Foo # for failsafe

This will force you to manually specify the test via a command-line argument when you want to run it, but omit the test otherwise.



来源:https://stackoverflow.com/questions/20637378/force-run-of-ignored-junit-test-using-maven-command-line

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