简体   繁体   English

Junit 和集成测试的最佳方法

[英]Junit and Integration Tests best approach

I want to make some integration test to test my whole program (it's a standart command line Java application with program args)我想做一些集成测试来测试我的整个程序(这是一个带有程序参数的标准命令行 Java 应用程序)

Basically I have 3 tests : one to create a resource, one to update the resource and finally one to delete it.基本上我有 3 个测试:一个是创建资源,一个是更新资源,最后一个是删除它。

I could do something like this :我可以做这样的事情:

@Test
public void create_resource() {
    MainApp.main(new String[] {"create", "my_resource_name"});
}

@Test
public void update_resource() {
    MainApp.main(new String[] {"update", "my_resource_name"});
}

@Test
public void delete_resource() {
    MainApp.main(new String[] {"delete", "my_resource_name"});
}

It works... as long as the methods are executed in the correct order.它可以工作......只要方法以正确的顺序执行。 I've heard that the good execution of a test should not depend of the order.我听说测试的良好执行不应该取决于顺序。

It's true that ordering tests is considered a smell.确实,订购测试被认为是一种气味。 Having said that, there might be cases where it might make sense, especially for integration tests.话虽如此,在某些情况下它可能是有意义的,尤其是对于集成测试。

Your sample code is a little vague since there are no assertions there.您的示例代码有点模糊,因为那里没有断言。 But it seems to me you could probably combine the three operation into a single test method.但在我看来,您可能可以将这三个操作组合成一个测试方法。 If you can't do that then you can just run them in order.如果你不能这样做,那么你可以按顺序运行它们。 JUnit 5 supports it using the @Order annotation: JUnit 5使用@Order注释支持它:

@TestMethodOrder(OrderAnnotation.class)
class OrderedTestsDemo {

    @Test
    @Order(1)
    void nullValues() {
        // perform assertions against null values
    }

    @Test
    @Order(2)
    void emptyValues() {
        // perform assertions against empty values
    }

    @Test
    @Order(3)
    void validValues() {
        // perform assertions against valid values
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM