当前位置:网站首页>JUnit test suite method sorting (method 2 is not easy to use)

JUnit test suite method sorting (method 2 is not easy to use)

2022-06-12 14:42:00 Micro blog

Test suite usage method 1 : There is no need to inherit

Be careful : Use it directly @SuiteClasses Node can execute multiple junit Use cases , As shown below , none_password.class, none_username.class It's all written junit class , Use a comma to separate them , The code is as follows

// Perform multiple unit tests

@RunWith(Suite.class)

@SuiteClasses({ none_password.class, none_username.class})

public class alltest {

}

Test suite usage method 2 :

Use the kit testsuite Multiple... Can be executed at once junit Test class , The usage is as follows

( Test class inheritance TestCase):    1、 Executes testJ2.class Inheritance TestCase  2、testJ2.class The test method in should be based on test( A lowercase letter ) start ( Otherwise you can't recognize junit, Report errors No tests found in) 3、 If there is some input or setup And so on. , Can be added to the front of the kit

package testforliuxw;

import junit.framework.Test;

import junit.framework.TestCase;

import junit.framework.TestSuite;

public class TestAll{

    public static Test suite(){

        TestSuite suite = new TestSuite();

        suite.addTestSuite(testJ2.class);

        suite.addTestSuite(testJmeter.class);

        return suite;        

    }

}

 

 

Test suite usage method 3 : Test methods can be filtered

Category:Category Also inherited from Suit,Category It seems to be Suit The enhanced , It and Suit It also provides the ability to organize several test case classes into a group , In addition, it can group test cases , Give you the opportunity to select only some of the use cases you need . First, we build two interface classes for the grouping of our test methods , as follows

public interface Inter1 {

}

public interface Inter2 {

}

Then we classify the corresponding test methods in the class where our test methods are located

test2 All methods in the class belong to Inter1 and Inter2 Two groups

@Category({Inter1.class,Inter2.class})

public class test2{

    @Test

    public void testxiao()

    {

        int i=1;

        assertEquals(1, i);

    }

}

test1 The first method in the class belongs to Inter1 Group , Second method Inter2 Belong to two groups

public class test3{

    @Category(Inter1.class)

    @Test

    public void testx()

    {

        int i=1;

        assertEquals(1, i);

    }

    @Category(Inter2.class)

    @Test

    public void testxi()

    {

        int i=1;

        assertEquals(2, i);    

    }

}

New test All3Test class , Added @IncludeCategory Limit only to Inter1 The use case , The running screenshot is as follows . Again Category And support @ExcludeCategory Annotations are used to exclude use cases , Use consistent , as follows , Only executed test3 Class

@RunWith(Categories.class)

@ExcludeCategory(Inter2.class) 

@SuiteClasses({

    test2.class,

    test3.class

  })

public class All3Test {

}

 

come from <http://lxw198902165221.blog.163.com/blog/static/25895002220164171042455/>

 

 

原网站

版权声明
本文为[Micro blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121418276157.html