如何重用单元测试进行集成测试

发布于 2024-11-09 18:52:00 字数 192 浏览 0 评论 0原文

我的应用程序有一个自定义服务器组件。

我们有一些针对业务逻辑的 JUnit 测试用例,并使用 JMockit 来模拟 DB 层。

对于集成测试,我们最终需要检查很多类似的条件。唯一的区别是我需要序列化内容并将其发送到套接字,而不仅仅是进行调用。

有没有任何(简单的)方法来概括测试,以便我可以为这两个测试指定一次这些条件?

My application has a custom server component.

We have some JUnit test cases for the business logic with JMockit in place to mock the DB layer.

For integration testing, we end up having a lot of similar conditions to check. The only difference is that I need to serialize stuff and send to a socket instead of just making a call.

Is there any (simple) way to generalize the tests so that I can specify those conditions once for both tests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿童谣 2024-11-16 18:52:00
 > Is there any (simple) way to generalize the tests so that I can 
 > specify those conditions once for both tests?

您可以使用构造辅助对象的虚拟 FactoryMethod 将测试放入基类中。

您的单元测试和集成测试继承自该类。 FactoryMethod 的实现会创建一些模拟对象或一些真实对象。

 > Is there any (simple) way to generalize the tests so that I can 
 > specify those conditions once for both tests?

You can put the tests into a baseclass with a virtual FactoryMethod that constructs helper-objects.

Your unittest und integrationtest inherits from that class. The implementation of the FactoryMethod creates either some mock or some real object.

嘦怹 2024-11-16 18:52:00

您可以将规则添加到一个文件中,您可以从集成测试和单元测试中引用该文件。

您应该用某种领域特定语言编写它,该语言可以由非技术利益相关者阅读,但也可以解析,例如:

discount: 10% for: orders over $10
discount: 20% for: orders over $40
discount: 30% for: orders over $100

You could add the rules to a file which you can reference from the integration test and the unit tests.

You should write it in some kind of domain specific language which can be read by non-technical stakeholders but can also be parsed, e.g.:

discount: 10% for: orders over $10
discount: 20% for: orders over $40
discount: 30% for: orders over $100
默嘫て 2024-11-16 18:52:00

您的单元和集成测试只是另一段代码,需要应用您的主流代码的标准概念。因此,编写可重用代码、使用设计模式、对接口进行编码以及最终持续重构之类的事情也适用于测试。

以下SO线程有更多建议:

  1. 在单元测试中重用代码
  2. 单元测试、集成测试或设计中的问题?

希望有帮助。

Your unit and integration tests are just another piece of code which would need to be applied the statndard concepts you have for your main stream code. So, things like writing reusable code, using design patterns, coding to interfaces and finally continuous refactoring would hold true for tests as well.

Following SO threads have more advice:

  1. Re-using code in unit tests
  2. Unit-Test, Integration test or problem in design?

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文