单元测试最佳实践

Unit tests are an important scaffold for large-scale software development; they enable us to design, write, and deploy production code with confidence by validating that software will behave as expected. Even though they may not execute in live systems, their development and maintenance requires the same care as general production code. Sometimes developers do not realize this which leads to testing code with more code smells than production code. Engineers may not give enough attention to test code changes in the code review process. However, most of the time the test code reflects the health of the production code. If the test code has some code smells, this can be a sign that the production code can be improved. In this post, I’m going to mention some of the best practices to keep unit test code clean and maximize the benefits they provide.

单元测试是大规模软件开发的重要基础。 它们通过验证软件的行为是否符合预期,使我们能够放心地设计,编写和部署生产代码。 即使它们可能无法在实时系统中执行,它们的开发和维护也需要与常规生产代码相同的注意。 有时开发人员没有意识到这一点,这导致测试代码比生产代码具有更多的代码味道 。 工程师可能不会在代码审查过程中对测试代码更改给予足够的重视。 但是,大多数情况下,测试代码反映了生产代码的运行状况。 如果测试代码具有某种代码味道,则可能表明可以改进生产代码。 在本文中,我将提到一些最佳实践,以保持单元测试代码的清洁并最大程度地发挥它们的作用。

The best practices for unit testing are debated topics in the industry. In practice, however, projects and teams should align on key concepts in order to foster code consistency and ease-of-maintenance. I’m going to mention the meaning of unit testing in the Object-Oriented design world, characteristics of a unit test, naming conventions for unit tests, and when we should or should not use mocking. We can have or find many different answers/approaches for these concepts, and the relevance of different trade-offs may vary depending on the situation.

单元测试的最佳实践是业界争论不休的话题。 但是在实践中,项目和团队应该在关键概念上保持一致,以促进代码的一致性和易维护性。 我将提到面向对象设计世界中单元测试的含义,单元测试的特征,单元测试的命名约定以及何时或不应该使用模拟。 对于这些概念,我们可以找到或找到许多不同的答案/方法,不同的权衡取舍的相关性可能会根据情况而有所不同。

单元测试 (Unit Testing)

To define unit testing, we should first define the unit. Once the unit has been defined, then we can define unit testing as testing the behaviors of a unit. Let’s try it for Object-Oriented software development methodology. Classes are the main building block of software designed with the Object-Oriented design paradigm. Then we can think that the class concept is the unit of Object-Oriented software, and unit testing involves independent testing of behaviors of a class by the developer who implements these behaviors.

要定义单元测试,我们应该首先定义单元。 一旦定义了单元,我们就可以将单元测试定义为测试单元的行为。 让我们尝试一下面向对象的软件开发方法。 类是使用面向对象设计范例设计的软件的主要构建块。 然后我们可以认为类概念是面向对象软件的单元,而单元测试则涉及实现这些行为的开发人员对类行为的独立测试。

A behavior and method relation of a class may not be 1:1. Sometimes a class can have more than one method to implement a behavior that is unit tested as a whole. Sometimes more than one class can be used to implement a behavior that’s unit tested. However, sometimes it can be a sign of code smell, like temporal coupling, if you use more than one public method or class to implement a unit tested behavior.

一个类的行为和方法关系可能不是1:1。 有时,一个类可以具有多个方法来实现作为一个整体进行单元测试的行为。 有时可以使用多个类来实现经过单元测试的行为。 但是,如果您使用多个公共方法或类来实现单元测试的行为,则有时可能是代码异味的征兆,例如时间耦合

单元测试的特征 (Characteristics of a Unit Test)

When developing unit tests, some key considerations include: How fast should a unit test be? How often should a unit test be run? Which kind of object methods are valid or not valid for unit testing? How should we structure a unit test? What kind of assertions should we make in a unit test? Let’s think about the answers to these questions.

在开发单元测试时,一些关键的考虑因素包括:单元测试应该有多快? 单元测试应该多久运行一次? 哪种对象方法对单元测试有效或无效? 我们应该如何构建单元测试? 我们应该在单元测试中做出什么样的断言? 让我们考虑一下这些问题的答案。

Some expected characteristics of unit tests include: fast execution times in order to provide immediate feedback about implementation correctness, readability in order to clearly express the behavior that’s tested, consistency and predictability of results through the use of deterministic evaluations, and robustness to structural changes (i.e., refactoring) in the implementation.

单元测试的一些预期特征包括:快速执行时间以提供有关实现正确性的即时反馈;可读性以清楚地表达所测试的行为;通过使用确定性评估来保证结果的一致性和可预测性;以及对结构更改的鲁棒性(即重构)。

速度 (Speed)

Developers expect unit tests to run quickly because they are genera

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值