3

junit 4 で TestSuite をパラメータ化することは可能ですか?

クラスをテスト スイートとして宣言するには、注釈@RunWith(Suite.class)が必要ですが、同じ注釈がテストをパラメーター化されたものとして宣言するためにも必要です。@RunWith(Parameterized.class)したがって、同じクラスに両方を追加することはできません。

このサイトで同様の質問を見つけましたが、あまり役に立ちませんでした。これまでのところ、私が見つけたすべての例は、完全なテスト スイートではなく、単純な単体テストをパラメーター化する方法を説明しています。

4

7 に答える 7

1

次のように、テスト スイートをパラメーター化し、スイートのテスト クラス メンバーでそのデータを使用することができました。

JUTsuite では:

@RunWith(Suite.class)
@Suite.SuiteClasses({ 
    JUT_test1.class,
})

public class JUTSuite{  
    // Declare all variables/objects you want to share with the test classes, e.g.
    protected static List<Fx> globalFxs;
    // This is the data list we'll use as parameters
    protected static List<Dx> globalDxs;

    @Parameters
    public static Collection<Object[]> data(){
        // Instantiate object list for parameters.  
        // Note: you must do it here and not in, say, @BeforeClass setup()
        // e.g.
        globalDxs=new ArrayList<Dx>(serverObj.values());

        Collection<Object[]> rows=new ArrayList<Object[]>();
        for(Dx d:globalDxs) {
            rows.add(new Object[]{d});
        }
        return rows;
    }

    @BeforeClass
    public static void setUp() throws Exception {
        // Instantiate/initialize all suite variables/objects to be shares with test classes
        // e.g. globalFxs=new ArrayList<Fx>();
    }

    @AfterClass
    public static void tearDown() throws Exception {
        // Clean up....
    }
}

次に、テストクラスで:

@RunWith(Parameterized.class)
public class JUT_test1 {
    // declare local names (if desired) for suite-wide variable/objects 
    // e.g. 
    private static List<Fx> globalFxs;

    // This is the test parameter:      
    private Dx d;

    public JUT_test1(Dx d){
        this.d=d;
    }

    @Parameters
    public static Collection<Object[]> data(){
    // Note: we're calling the suite's data() method which has already executed.
        return JUTSuite.data();
    }

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    // (If desired)initialize local variables by referencing suite variables.
    // e.g.globalFxs=JUTSuite.globalFxs;
    }
}
于 2015-01-15T03:04:48.643 に答える
0

その通りです。 と は両方ともSuiteランナーParameterizedでありRunner、一度にテストを実行するために使用できるのは 1 つだけです。標準の JUnit 4 は、結合されたランナーを提供しません。

独自のランナーを実装するか、ParameterizedSuiteランナーを提供するこのすぐに使用できるライブラリを見ることができます: https://github.com/PeterWippermann/parameterized-suite

パラメータ化されたテスト スイートは次のようになります。

@RunWith(ParameterizedSuite.class)
@SuiteClasses({OneTest.class, TwoTest.class})
public class MyParameterizedTestSuite {
    @Parameters(name = "Parameters are {0} and {1}")
    public static Object[] params() {
        return new Object[][] {{'A',1}, {'B',2}, {'C',3}};
    }
于 2016-07-03T21:22:31.927 に答える
0

最良の解決策は、空白のクラスでスーツ クラスを個別に保持することです。たとえば、パラメータ化されたテストとしてログインをテストし、スーツを着ています(ナビゲーションのパフォーマンス測定用)

     @RunWith(Suite.class)
@Suite.SuiteClasses({
            LoginPageTest.class,
            HomePageTests.class})
    public class PerformanceTests {
    }

LoginPageTest は実際には Parameterizedtests です

@RunWith(Parameterized.class)
public class LoginPageTest
{...}
于 2016-08-09T12:57:54.617 に答える