美文网首页
马桶测试-1: 创建干净的测试数据

马桶测试-1: 创建干净的测试数据

作者: 无不可无可 | 来源:发表于2018-03-07 20:20 被阅读0次

马桶测试-1: 创建干净的测试数据

Google Testing on the Toilet Series 翻译第一篇,创建干净的测试数据

原文 Origin Testing on the Toilet: Cleanly Create Test Data

Helper方法让创建数据非常方便,但是随着时间的推移,不同的变量数据创建方法加入之后,代码可读性变得不那么好了.

// This helper method starts with just a single parameter:
Company company = newCompany(PUBLIC);

// But soon it acquires more and more parameters.
// Conditionals creep into the newCompany() method body to handle the nulls,
// and the method calls become hard to read due to the long parameter lists:
Company small = newCompany(2, 2, null, PUBLIC);
Company privatelyOwned = newCompany(null, null, null, PRIVATE);
Company bankrupt = newCompany(null, null, PAST_DATE, PUBLIC);

// Or a new method is added each time a test needs a different combination of fields:
Company small = newCompanyWithEmployeesAndBoardMembers(2, 2, PUBLIC);
Company privatelyOwned = newCompanyWithType(PRIVATE);
Company bankrupt = newCompanyWithBankruptcyDate(PAST_DATE, PUBLIC);

为了让测试数据代码更据可读性,使用builder pattern,通过返回部分数据的builder类来创建数据,修改不同的状态是一个不错的方法。 创建数据的方法可以生成部分的默认数据,之后再修改需要修改的数据,可以让测试代码方便而且容易阅读,如下例:

Company small = newCompany().setEmployees(2).setBoardMembers(2).build();
Company privatelyOwned = newCompany().setType(PRIVATE).build();
Company bankrupt = newCompany().setBankruptcyDate(PAST_DATE).build();
Company arbitraryCompany = newCompany().build();

// Zero parameters makes this method reusable for different variations of Company.
// It also doesn’t need conditionals to ignore parameters that aren’t set (e.g. null
// values) since a test can simply not set a field if it doesn’t care about it.
private static Company.Builder newCompany() {
  return Company.newBuilder().setType(PUBLIC).setEmployees(100); // Set required fields
}

同时测试不应该以来任何的默认值,如果有依赖就让使用者必须去了解具体的创建数据方法的实现了。

// This test needs a public company, so explicitly set it.
// It also needs a company with no board members, so explicitly clear it.
Company publicNoBoardMembers = newCompany().setType(PUBLIC).clearBoardMembers().build();

更多详细信息可以参考:http://www.natpryce.com/articles/000714.html

附上原文:

This article was adapted from a Google Testing on the Toilet (TotT) episode. You can download a printer-friendly version of this TotT episode and post it in your office.
By Ben Yu
Helper methods make it easier to create test data. But they can become difficult to read over time as you need more variations of the test data to satisfy constantly evolving requirements from new tests:

// This helper method starts with just a single parameter:
Company company = newCompany(PUBLIC);

// But soon it acquires more and more parameters.
// Conditionals creep into the newCompany() method body to handle the nulls,
// and the method calls become hard to read due to the long parameter lists:
Company small = newCompany(2, 2, null, PUBLIC);
Company privatelyOwned = newCompany(null, null, null, PRIVATE);
Company bankrupt = newCompany(null, null, PAST_DATE, PUBLIC);

// Or a new method is added each time a test needs a different combination of fields:
Company small = newCompanyWithEmployeesAndBoardMembers(2, 2, PUBLIC);
Company privatelyOwned = newCompanyWithType(PRIVATE);
Company bankrupt = newCompanyWithBankruptcyDate(PAST_DATE, PUBLIC);

Instead, use the test data builder pattern: create a helper method that returns a partially-built object (e.g., a Builder in languages such as Java, or a mutable object) whose state can be overridden in tests. The helper method initializes logically-required fields to reasonable defaults, so each test can specify only fields relevant to the case being tested:

Company small = newCompany().setEmployees(2).setBoardMembers(2).build();
Company privatelyOwned = newCompany().setType(PRIVATE).build();
Company bankrupt = newCompany().setBankruptcyDate(PAST_DATE).build();
Company arbitraryCompany = newCompany().build();

// Zero parameters makes this method reusable for different variations of Company.
// It also doesn’t need conditionals to ignore parameters that aren’t set (e.g. null
// values) since a test can simply not set a field if it doesn’t care about it.
private static Company.Builder newCompany() {
  return Company.newBuilder().setType(PUBLIC).setEmployees(100); // Set required fields
}

Also note that tests should never rely on default values that are specified by a helper method since that forces readers to read the helper method’s implementation details in order to understand the test.

// This test needs a public company, so explicitly set it.
// It also needs a company with no board members, so explicitly clear it.
Company publicNoBoardMembers = newCompany().setType(PUBLIC).clearBoardMembers().build();

You can learn more about this topic at http://www.natpryce.com/articles/000714.html

相关文章

  • 马桶测试-1: 创建干净的测试数据

    马桶测试-1: 创建干净的测试数据 Google Testing on the Toilet Series 翻译第...

  • 测试数据

    解决测试数据解耦 1.测试数据准备 (1)测试输入数据 (2)提前准备的测试数据 2.创建数据 (1)创建方法 a...

  • Robot Framework官方教程(二)测试数据语法

    2.1.1文件和目录 测试数据的层次结构安排如下: 测试数据在测试数据文件中创建。 测试数据文件会自动创建一个包含...

  • MySQL生成大量测试数据方法

    Mysql创建测试大量测试数据 修改mysql配置 创建测试数据库 创建数据表 创建随机字符串函数 创建存储过程 ...

  • 目录

    1 新手入门 1.1 介绍 1.2 版权许可 1.3 安装说明 1.4 示范 2 创建测试数据 2.1 测试数据语...

  • Postgresql 加解密操作

    创建加密扩展 创建测试表 插入测试数据 查询语句

  • 03搭建MyBatis

    搭建MyBatis项目 1、下载MyBatis、创建项目、导包; 2、创建测试用例,测试数据库、测试Bean对象;...

  • 在rails中创建测试数据的几种方法

    一、创建测试数据 在rails中我们经常需要创建测试数据来测试功能有没有实现,除了在rails的console中创...

  • 准备测试数据的痛点

    文章内容来源于《软件测试52讲》 在什么时机创建这些测试数据合适呢?比如,是在测试用例中实时创建测试数据,还是在准...

  • MySql 索引理解及测试

    准备 1.创建如下表结构 2.添加百万条测试数据,注意数据的分散性 测试 1.创建 account 单列索引 1....

网友评论

      本文标题:马桶测试-1: 创建干净的测试数据

      本文链接:https://www.haomeiwen.com/subject/bqusfftx.html