Useful design patterns for unit testing/TDD?

后端 未结 9 658
一个人的身影
一个人的身影 2021-01-29 17:54

Reading this question has helped me solidify some of the problems I\'ve always had with unit-testing, TDD, et al.

Since coming across the TDD approach to development I k

9条回答
  •  清歌不尽
    2021-01-29 18:33

    Here Mike Clifton describes 24 test patterns from 2004. Its a useful heuristic when designing unit tests.

    http://www.codeproject.com/Articles/5772/Advanced-Unit-Test-Part-V-Unit-Test-Patterns

    Pass/Fail Patterns

    These patterns are your first line of defence (or attack, depending on your perspective) to guarantee good code. But be warned, they are deceptive in what they tell you about the code.

    • The Simple-Test Pattern
    • The Code-Path Pattern
    • The Parameter-Range Pattern

    Data Transaction Patterns

    Data transaction patterns are a start at embracing the issues of data persistence and communication. More on this topic is discussed under "Simulation Patterns". Also, these patterns intentionally omit stress testing, for example, loading on the server. This will be discussed under "Stress-Test Patterns".

    • The Simple-Data-I/O Pattern
    • The Constraint-Data Pattern
    • The Rollback Pattern

    Collection Management Patterns

    A lot of what applications do is manage collections of information. While there are a variety of collections available to the programmer, it is important to verify (and thus document) that the code is using the correct collection. This affects ordering and constraints.

    • The Collection-Order Pattern
    • The Enumeration Pattern The
    • Collection-Constraint Pattern
    • The Collection-Indexing Pattern

    Performance Patterns

    Unit testing should not just be concerned with function but also with form. How efficiently does the code under test perform its function? How fast? How much memory does it use? Does it trade off data insertion for data retrieval effectively? Does it free up resources correctly? These are all things that are under the purview of unit testing. By including performance patterns in the unit test, the implementer has a goal to reach, which results in better code, a better application, and a happier customer.

    • The Performance-Test Pattern

    Process Patterns

    Unit testing is intended to test, well, units...the basic functions of the application. It can be argued that testing processes should be relegated to the acceptance test procedures, however I don't buy into this argument. A process is just a different type of unit. Testing processes with a unit tester provide the same advantages as other unit testing--it documents the way the process is intended to work and the unit tester can aid the implementer by also testing the process out of sequence, rapidly identifying potential user interface issues as well. The term "process" also includes state transitions and business rules, both of which must be validated.

    • The Process-Sequence Pattern
    • The Process-State Pattern
    • The Process-Rule Pattern

    Simulation Patterns

    Data transactions are difficult to test because they often require a preset configuration, an open connection, and/or an online device (to name a few). Mock objects can come to the rescue by simulating the database, web service, user event, connection, and/or hardware with which the code is transacting. Mock objects also have the ability to create failure conditions that are very difficult to reproduce in the real world--a lossy connection, a slow server, a failed network hub, etc.

    • Mock-Object Pattern
    • The Service-Simulation Pattern
    • The Bit-Error-Simulation Pattern
    • The Component-Simulation Pattern

    Multithreading Patterns

    Unit testing multithreaded applications is probably one of the most difficult things to do because you have to set up a condition that by its very nature is intended to be asynchronous and therefore non-deterministic. This topic is probably a major article in itself, so I will provide only a very generic pattern here. Furthermore, to perform many threading tests correctly, the unit tester application must itself execute tests as separate threads so that the unit tester isn't disabled when one thread ends up in a wait state

    • The Signalled Pattern
    • The Deadlock-Resolution Pattern

    Stress-Test Patterns

    Most applications are tested in ideal environments--the programmer is using a fast machine with little network traffic, using small datasets. The real world is very different. Before something completely breaks, the application may suffer degradation and respond poorly or with errors to the user. Unit tests that verify the code's performance under stress should be met with equal fervor (if not more) than unit tests in an ideal environment.

    • The Bulk-Data-Stress-Test Pattern
    • The Resource-Stress-Test Pattern
    • The Loading-Test Pattern

    Presentation Layer Patterns

    One of the most challenging aspects of unit testing is verifying that information is getting to the user right at the presentation layer itself and that the internal workings of the application are correctly setting presentation layer state. Often, presentation layers are entangled with business objects, data objects, and control logic. If you're planning on unit testing the presentation layer, you have to realize that a clean separation of concerns is mandatory. Part of the solution involves developing an appropriate Model-View-Controller (MVC) architecture. The MVC architecture provides a means to develop good design practices when working with the presentation layer. However, it is easily abused. A certain amount of discipline is required to ensure that you are, in fact, implementing the MVC architecture correctly, rather than just in word alone.

    • The View-State Test Pattern
    • The Model-State Test Pattern

提交回复
热议问题