简体   繁体   English

TDD测试应用入口点

[英]TDD testing application entry points

I'm new to TDD, and quite like it so far (even though it's an adjustment process). 我是TDD的新手,到目前为止非常喜欢它(尽管这是一个调整过程)。

However, I'm struggling with how to go about testing the entry points to the application. 但是,我正在努力研究如何测试应用程序的入口点。 By those I mean the main(String... args) method, a class whose single responsibility is to load and save a properties file from/to disk, etc. 我指的是main(String... args)方法,这个类的唯一职责是从/向磁盘加载和保存属性文件等。

When I was doing code first, this didn't seem like a problem. 当我第一次做代码时,这似乎不是问题。 I just wouldn't write test for those kind of methods/classes. 我只是不会为那些方法/类编写测试。 However, how am I supposed to follow the TDD process when there's code I have to write without tests? 但是,当我必须在没有测试的情况下编写代码时,我应该如何遵循TDD流程?

In other words, when/how does writing the entry points fit into the TDD development process? 换句话说,何时/如何编写入口点适合TDD开发过程?

In most cases, you can't unit test the entry point, because, by definition, a unit test tests a unit in isolation from its dependencies. 在大多数情况下,您无法对入口点进行单元测试,因为根据定义, 单元测试会单独测试单元与其依赖关系。

In all but some trivial edge cases, the application's entry point is the Composition Root - that is, the place where all units are integrated. 除了一些微不足道的边缘情况之外,应用程序的入口点是组合根 - 即所有单元集成的位置。 While you could perform a full systems test on the entry point, usually you should treat it as a Humble Executable . 虽然您可以对入口点执行完整的系统测试,但通常应将其视为Humble Executable

My personal rule is that the Humble Executable can contain no logic (only composition), and I measure that with Cyclomatic Complexity ; 我个人的规则是,Humble Executable不能包含任何逻辑(只有合成),我用Cyclomatic Complexity来衡量它; if the Cyclomatic Complexity of the Humble Executable is 1, it's okay not to test it. 如果Humble Executable的Cyclomatic Complexity是1,那么可以不测试它。

However, although you can't unit test the entry points, you can still use Outside-In TDD to drive the composition, although you'd typically still need to replace some external services with Test Doubles . 但是,尽管您无法对入口点进行单元测试,但您仍然可以使用Outside-In TDD来驱动组合,尽管您通常仍需要使用Test Doubles替换一些外部服务。

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

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