`
experience
  • 浏览: 196571 次
社区版块
存档分类
最新评论
阅读更多

I've noticed two styles I've used on my last, first TDD, project. The first is from when I was newer, and I would write one, absolute minimum test and make it pass, then move on to another test. Now, still taking small steps, I often find myself writing a complete test, with say five asserts, and then incrementally adding to the method under test to progress past each assert, until all pass. Any comments on this approach?

============

I've done both, and I feel that the flow is better with tiny tests - but I still find myself doing incremental tests from time to time. One technique that I've found helpful in keeping the tiny test approach fluid is use of setup() - and that got easier after Dave Astel's book taught me to write test classes per purpose, rather than per class. So tests that need different setups wind up in different test classes. When I was still in one-test-class-per-class mode, I was more inclined to write bigger test functions, because what belonged in setup was getting duplicated in each test. Utility functions also come in handy - encapsulating the assertions that you want to make more generally. For me, there's kind of a tradeoff between utility functions and one-test-per-purpose, because if I want the utility function to do assertions, then it should belong to a subclass of TestCase, and if I want my test classes to call those functions directly, then they want to subclass my utility class, and that soon becomes more structure than I want in my test classes.

================

One test-class per purpose, and not per class? Please elucidate. What defines a purpose, and how do you organize your tests? Does one class have many purposes, or does one purpose span many classes?

===============

A simplistic, mechanical answer - and one that serves me reasonably well - is that the setup defines the purpose. If I need different setup, that drives me to create a different test class. I don't have Astels' book in front of me, but I do recommend it for its explanation of this, much better than I can do here. My practice is not as absolute as maybe my remarks suggested - sometimes my test classes will include cases with different (inline) setup. But when I see too much of that, or duplicated setup in multiple tests - that's the code asking me to sprout a new test class. Or a utility function, depending. On what, I can't really say.

Well - the tests are never far from the class (same package, suffixed .test), and Eclipse lets me find references. So I never have a problem finding the test class. Regarding naming - take Range, for example. I might have RangeTest, covering contains(), isEmpty(), encompass(), which setup includes a single Range, or a few Ranges for which the functions can be evaluated, plus RangeInteractionTest, which tests intersects(), intersection(), and other - well, Range Interactions. I'd have to look at my code to find real examples; no time right now. But that should give you an idea. Really, though - check out the book.

============

from Dave Astels' Test-Driven Development: A Practical Guide, pp 74-76: Let's begin by considering TestCase. It is used to group related tests together. But what does "related" mean? It is often misunderstood to mean all tests for a specific class or specific group of related classes. This misunderstanding is reinforced by some of the IDE plug-ins that will generate a TestCase for a specified class, creating a test method for each method in the target class. These test creation facilities are overly simplistic at best, and misleading at worst. They reinforce the view that you should have a TestCase for each class being tested, and a test for each method in those classes. But that approach has nothing to do with TDD, so we won't discuss it further. This structural correspondence of tests misses the point. You should write tests for behaviors, not methods. A test method should test a single behavior... ...TestCase is a mechanism to allow _fixture_ reuse. Each TestCase subclass represents a fixture, and contains a group of tests that run in the context of that fixture. A fixture is the set of preconditions and assumptions with which a test is run. It is the runtime context for the test, embodied in the instance variables of the TestCase, the code in the setUp() method, and any variables and setup code local to the test method... ...Instead of using TestCase to group tests for a given class, try thinking about it as a way to group tests that need to be set up in exactly the same way. A measure of how well your TestCase is mapping to the requirements of a single fixture is how uniformly that fixture (as described by the setUp() method) is used by all of the test methods. Whenever you discover that your setUp() method contains code for some of your test methods, and different code for other test methods, consider it a smell that indicates that you should refactor the TestCase into two or more TestCases. Once you get the hang of defining TestCases this narrowly, you will find that they are easier to understand and maintain.

分享到:
评论

相关推荐

    ratherthan用法小结ratherthan是一个并列连词.doc

    "I decided to write rather than (to) telephone."(我决定写信而不是打电话。)当`rather than`位于句首时,不定式通常不带`to`,如"Rather than allow the vegetables to go bad, he sold them at half price....

    扩容U盘检测工具Udisk Write Test软件UWriteTest

    **扩容U盘检测工具Udisk Write Test软件UWriteTest** 在数字时代,U盘作为便携式存储设备,被广泛用于数据传输和备份。然而,市场上的U盘质量参差不齐,有些商家为了降低成本,可能会销售所谓的“扩容U盘”,即实际...

    BURNINTEST--硬件检测工具

    PassMark BurnInTest V5.3 Copyright (C) 1999-2008 PassMark Software All Rights Reserved http://www.passmark.com Overview ======== Passmark's BurnInTest is a software tool that allows all the major sub...

    Test What you Write - Ship What you Test

    该文档介绍了SOA和Heterogeneity中的相关内容,告诉我们什么是Dotci,并详细的介绍了Docker和Dotci的共同应用,在此基础上,简介了Docker和Dotci的常用工具和环境。

    java 小作业01 Write a class Person.

    We need to model the concept of...We can model parents as instances of the Person class and children as a collection of Person instances,. At least, the Family class should provide the following methods:

    test_sd.rar_SD FAT16_SD_TEST_Write On_test_sd.rar_write sd card

    标题中的“test_sd.rar_SD FAT16_SD_TEST_Write On_test_sd.rar_write sd card”表明这是一个与SD卡相关,特别是FAT16文件系统相关的测试项目,用于在SD卡上进行写入操作。描述中提到的“a simple program to write ...

    UdiskWriteTest(扩容U盘检测工具)V1.0绿色免费版

    Udisk Write Test是一款强大的扩容U盘检测工具。用户可以通过该软件测试U盘是否被奸商扩容。该软件的作用就是通过测试还原U盘容量的真实面目,检测写入数据的完整性。 Udisk Write Test 使用说明: 打开,软件会...

    Effective Software Test Automation

    Today, software organizations invest more time and resources in analyzing and testing software as a unit rather than as independent entities. Software engineers have observed that writing testing ...

    Building Maintainable Software, Java Edition(O'Reilly,2016)

    Write code once, rather than risk copying buggy code Keep unit interfaces small by extracting parameters into objects Separate concerns to avoid building large classes Couple architecture components ...

    Building Maintainable Software, C# Edition [2016]

    Write code once, rather than risk copying buggy code Keep unit interfaces small by extracting parameters into objects Separate concerns to avoid building large classes Couple architecture components ...

    高中英语必修三unitlanguage pointsPPT学习教案.pptx

    - "I decided to write rather than (to) telephone." (我决定写信而不是打电话。) - "You rather than I am going to go camping." (是你而不是我要去露营。) 3. **would rather 和 prefer 的用法** - ...

    Write To File Speed Test - 副本_labview_

    "Write To File Speed Test - 副本_labview_" 是一个专门针对LabVIEW的文件读写速度测试程序,旨在帮助用户了解和优化他们的文件操作性能。 首先,我们需要理解LabVIEW中的文件I/O操作。LabVIEW提供了丰富的文件...

    一个公共类Test在其中的main函数中使用Pen类定义对象

    ①定义类的头部 class Pen ②定义类的主体部分,包括变量和方法:变量如colo r,length,price等;方法如Write(),GetPrice()等 ③定义多个构造方法,如Pen(),Pen(参数列表) 2. 定义对象的方法 ①定义公共类Test,则保存...

    Read and Write Utility

    Read and Write Utility

    gpio-test.rar_GPIO_GPIO_Test_gpio test_linux gpio_test-gpio

    GPIO,全称General Purpose Input/Output,是通用输入输出接口,广泛应用于嵌入式系统设计中,如微控制器(MCU)和系统级芯片(SoC)。GPIO接口允许硬件开发者控制和监测系统的数字信号,使其能灵活地与其他硬件组件...

    eeprom_test.zip_Write On_pic16f84

    标题 "eeprom_test.zip_Write On_pic16f84" 提到的是一个针对 PIC16F84 微控制器的EEPROM(电可擦可编程只读存储器)读写测试程序,而“write_on”标签进一步强调了该程序重点在于写入操作。下面将详细介绍与这个...

    Testbench常用库函数用法.pdf

    在数字电路设计和仿真中,Testbench(测试平台)是一个至关重要的组件,它用于模拟电路的运行环境,并提供验证设计是否符合规格的手段。在Testbench中,常用的库函数可以分为两大类:显示、打印任务的系统函数,以及...

    Test_fconst_2.rar_class A

    Base class for instructions which are of a fixed code size and which use InsnFormat methods to write themselves.

Global site tag (gtag.js) - Google Analytics