Write Tests for People

本文强调了在编写生产代码时,如何撰写高质量的测试以供理解代码的人使用。测试应作为代码的文档,清晰描述代码的工作方式,并通过明确的上下文、调用方式和预期结果来帮助理解不同场景下的代码行为。此外,测试的命名和组织方式应便于读者快速了解其目的和应用场景。

You are writing automated tests for some or all of your production code. Congratulations! You are writing your tests before you write the code? Even better!! Just doing this makes you one of the early adopters on the leading edge of software engineering practice. But are you writing good tests? How can you tell? One way is to ask "Who am I writing the tests for?" If the answer is "For me, to save me the effort of fixing bugs" or "For the compiler, so they can be executed" then the odds are you aren't writing the best possible tests. So who should you be writing the tests for? For the person trying to understand your code.

Good tests act as documentation for the code they are testing. They describe how the code works. For each usage scenario the test(s):

  1. Describe the context, starting point, or preconditions that must be satisfied
  2. Illustrate how the software is invoked
  3. Describe the expected results or postconditions to be verified

Different usage scenarios will have slightly different versions of each of these. The person trying to understand your code should be able to look at a few tests and by comparing these three parts of the tests in question, be able to see what causes the software to behave differently. Each test should clearly illustrate the cause and effect relationship between these three parts. This implies that what isn't visible in the test is just as important as what is visible. Too much code in the test distracts the reader with unimportant trivia. Whenever possible hide such trivia behind meaningful method calls — the Extract Method refactoring is your best friend. And make sure you give each test a meaningful name that describes the particular usage scenario so the test reader doesn't have to reverse engineer each test to understand what the various scenarios are. Between them, the names of the test class and class method should include at least the starting point and how the software is being invoked. This allows the test coverage to be verified via a quick scan of the method names. It can also be useful to include the expected results in the test method names as long as this doesn't cause the names to be too long to see or read.

It is also a good idea to test your tests. You can verify they detect the errors you think they detect by inserting those errors into the production code (your own private copy that you'll throw away, of course). Make sure they report errors in a helpful and meaningful way. You should also verify that your tests speak clearly to a person trying to understand your code. The only way to do this is to have someone who isn't familiar with your code read your tests and tell you what they learned. Listen carefully to what they say. If they didn't understand something clearly it probably isn't because they aren't very bright. It is more likely that you weren't very clear. (Go ahead and reverse the roles by reading their tests!)


by Gerard Meszaros

This work is licensed under a Creative Commons Attribution 3

### Gradle 测试执行问题:`no tests found for given includes` 的解决方案 在使用 Gradle 执行测试时,如果遇到提示 `no tests found for given includes`,这通常意味着 Gradle 无法找到符合指定过滤条件的测试类或方法。以下是一些常见的原因和对应的解决办法: #### 1. 测试类未被正确识别 Gradle 默认会在 `src/test/java` 目录下查找测试类。如果测试类没有放置在该目录,或者类名不符合默认的命名规范(如 `*Test`, `*TestCase`),则 Gradle 会跳过这些类。 ```groovy test { include '**/*Tests.java' // 自定义匹配规则 } ``` 可以通过在 `build.gradle` 中自定义 `include` 模式来匹配特定的测试类路径和命名规则 [^1]。 #### 2. 使用了错误的测试框架配置 如果项目中使用了 JUnit 5,但未正确配置 `useJUnitPlatform()`,则 Gradle 可能无法识别测试类。 ```groovy test { useJUnitPlatform() } ``` 类似地,如果使用的是 TestNG,应配置 `useTestNG()` 并可选地设置测试组过滤条件 [^1]。 #### 3. 测试类未被编译 Gradle 测试任务依赖于编译后的测试类文件。如果测试类未被正确编译(如构建失败、未执行 `testClasses` 任务),则测试任务无法找到测试类。 确保执行了完整的构建流程: ```bash ./gradlew build ``` 或仅执行测试类的编译任务: ```bash ./gradlew testClasses ``` #### 4. 使用了 `--tests` 参数但未匹配到任何测试 当使用 `--tests` 参数运行测试时,Gradle 会根据提供的类名或方法名进行匹配。如果提供的名称不准确或大小写不一致,可能导致匹配失败。 ```bash ./gradlew test --tests "com.example.MyTest.myTestMethod" ``` 确保类名和方法名完全匹配,包括包名和大小写 [^1]。 #### 5. 多模块项目中未正确配置子模块 在多模块项目中,若未正确设置 `settings.gradle` 或未在子模块中定义测试任务,可能导致主模块无法识别子模块的测试类。 确保 `settings.gradle` 包含所有子模块: ```groovy include(":module1") include(":module2") ``` 并在每个子模块的 `build.gradle` 中配置测试任务。 #### 6. 使用了 TestNG 并设置了错误的测试组过滤条件 如果使用了 TestNG,并设置了 `includeGroups` 和 `excludeGroups`,但测试类中未使用相应的 `@Test(groups = "unitTests")` 注解,则可能导致无测试匹配。 ```groovy test { useTestNG { includeGroups 'unitTests' excludeGroups 'integrationTests' } } ``` 确保测试类中使用了对应的测试组注解 。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值