Exclude some JUnit tests from automated test suite

后端 未结 4 1183
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-19 06:54

When writing code that interacts with external resources (such as using a web service or other network operation), I often structure the classes so that it can also be \"stu

相关标签:
4条回答
  • 2020-12-19 07:45

    You can use @Ignore annotation to prevent them from running automatically during test. If required, you may trigger such Ignored tests manually.

        @Test
        public void wantedTest() {
            return checkMyFunction(10);
        }
    
        @Ignore
        @Test
        public void unwantedTest() {
            return checkMyFunction(11);
        }
    

    In the above example, unwantedTest will be excluded.

    0 讨论(0)
  • 2020-12-19 07:48

    As far as I can see you use gradle and API for JUnit says that annotation @Ignore disables test. I will add gradle task which will add @Ignore for those tests.

    0 讨论(0)
  • 2020-12-19 07:57

    I recommend using junit categories. See this blog for details : https://community.oracle.com/blogs/johnsmart/2010/04/25/grouping-tests-using-junit-categories-0.

    Basically, you can annotate some tests as being in a special category and then you can set up a two test suites : one that runs the tests of that category and one that ignores tests in that category (but runs everything else)

    @Category(IntegrationTests.class)
    public class AccountIntegrationTest {
    
      @Test
      public void thisTestWillTakeSomeTime() {
        ...
       }
    
      @Test
      public void thisTestWillTakeEvenLonger() {
         ....
      }
    

    }

    you can even annotate individual tests"

    public class AccountTest {
    
       @Test
       @Category(IntegrationTests.class)
       public void thisTestWillTakeSomeTime() {
         ...
        }
    

    Anytime I see something manually getting turned on or off I cringe.

    0 讨论(0)
  • 2020-12-19 07:57

    If you're just wanting to disable tests for functionality that hasn't been written yet or otherwise manually disable some tests temporarily, you can use @Ignore; the tests will be skipped but still noted in the report.

    If you are wanting something like Spring Profiles, where you can define rulesets for which tests get run when, you should either split up your tests into separate test cases or use a Filter.

    0 讨论(0)
提交回复
热议问题