Java 使用 JUnit 的 @Parameterized 时,我可以让一些测试仍然只运行一次吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32776335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:04:16  来源:igfitidea点击:

When using JUnit's @Parameterized, can I have some tests still run only once

javajunitjunit4parameterized

提问by centic

I use @Parameterizedin many cases to run tests on a number of permutations. This works very well and keeps the test-code itself simple and clean.

@Parameterized在许多情况下使用对许多排列进行测试。这非常有效并且保持测试代码本身简单和干净。

However sometimes I would like to have some of the test-methods still run only once as they do not make use of the parameters, is there a way with JUnit to mark the test-method as "singleton" or "run-once"?

但是有时我希望某些测试方法仍然只运行一次,因为它们不使用参数,JUnit 有没有办法将测试方法标记为“单一”或“运行一次”?

Note: This does not concern running single tests in Eclipse, I know how to do that :)

注意:这与在 Eclipse 中运行单个测试无关,我知道该怎么做:)

采纳答案by daver

You can associate any number of test classes to run together using a suite. This way all the tests are run when you test your class and you can mix different test runners.

您可以使用套件关联任意数量的测试类以一起运行。这样,当你测试你的类时所有的测试都会运行,你可以混合不同的测试运行器。

  1. Create a test suite associated with the class you are testing
  2. Add a reference to the parameterized test class
  3. Add the other class(es) containing non parameterized tests.

    import org.junit.runners.Suite;
    import org.junit.runner.RunWith;
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses({ParameterizedTestClass.class, UnitTests.class, MoreUnitTests.class})
    public class SutTestSuite{
         //Empty...
    }
    
  1. 创建与您正在测试的类相关联的测试套件
  2. 添加对参数化测试类的引用
  3. 添加包含非参数化测试的其他类。

    import org.junit.runners.Suite;
    import org.junit.runner.RunWith;
    
    @RunWith(Suite.class)
    @Suite.SuiteClasses({ParameterizedTestClass.class, UnitTests.class, MoreUnitTests.class})
    public class SutTestSuite{
         //Empty...
    }
    

回答by piotrek

there is a number of junit plugins that give you some more features/power regarding parameterized tests. check zohhak, junit-parames and junit-dataprovider. they allow you to mix parametrized and simple junit tests

有许多 junit 插件可以为您提供有关参数化测试的更多功能/功能。检查 zohhak、junit-parames 和 junit-dataprovider。它们允许您混合参数化和简单的 junit 测试

回答by Stefan Birkner

You could structure your test with the Enclosed runner.

您可以使用Enclosed runner来构建您的测试。

@RunWith(Enclosed.class)
public class TestClass {

    @RunWith(Parameterized.class)
    public static class TheParameterizedPart {

        @Parameters
        public static Object[][] data() {
            ...
        }

        @Test
        public void someTest() {
            ...
        }

        @Test
        public void anotherTest() {
            ...
        }
    }

    public static class NotParameterizedPart {
        @Test
        public void someTest() {
            ...
        }
    }
}

回答by PFROLIM

Before I knew about "@RunWith(Enclosed.class)" approach, I used the following (similar) solution, with inner classes extending outer class. I keep using this structure because I like that the tests are in same place and share some properties and methods and things seems clearer to me. Then, using Eclipse, in my run configuration, I choose that option "Run all tests in the selected project, package or source folder" and all these tests will be performed with just a click.

在我了解“@RunWith(Enclosed.class)”方法之前,我使用了以下(类似的)解决方案,内部类扩展了外部类。我一直使用这种结构,因为我喜欢测试在同一个地方并共享一些属性和方法,事情对我来说似乎更清晰。然后,使用 Eclipse,在我的运行配置中,我选择该选项“在选定的项目、包或源文件夹中运行所有测试”,只需单击一下即可执行所有这些测试。

public class TestBooksDAO {

    private static BooksDAO dao;

    @Parameter(0)
    public String title;
    @Parameter(1)
    public String author;

    @Before
    public void init() {
        dao = BooksDAO.getInstancia();
    }

    /** Tests that run only once. */
    public static class SingleTests extends TestBooksDAO {

        @Test(timeout=10000)
        public void testGetAll() {
            List<Book> books = dao.getBooks();
            assertNotNull(books);
            assertTrue(books.size()>0);
        }

        @Test(timeout=10000)
        public void testGetNone() {
            List<Book> books = dao.getBooks(null);
            assertNull(books);
        }
    }

    /** Tests that run for each set of parameters. */
    @RunWith(Parameterized.class)
    public static class ParameterizedTests1 extends TestBooksDAO {

        @Parameters(name = "{index}: author=\"{2}\"; title=\"{0}\";")
        public static Collection<Object[]> values() {
            return Arrays.asList(new Object[][] { 
                {"title1", ""}, 
                {"title2", ""}, 
                {"title3", ""}, 
                {"title4", "author1"}, 
                {"title5", "author2"}, 
            });
        }

        @Test(timeout=10000)
        public void testGetOneBook() {
            Book book = dao.getBook(author, title);
            assertNotNull(book);
        }
    }

    /** Other parameters for different tests. */
    @RunWith(Parameterized.class)
    public static class ParameterizedTests2 extends TestBooksDAO {

        @Parameters(name = "{index}: author=\"{2}\";")
        public static Collection<Object[]> values() {
            return Arrays.asList(new Object[][] { 
                {"", "author1"}, 
                {"", "author2"}, 
                {"", "author3"}, 
            });
        }

        @Test(timeout=10000)
        public void testGetBookList() {
            List<Book> books = dao.getBookByAuthor(author);
            assertNotNull(books);
            assertTrue(books.size()>0);
        }
    }
}