Writing readable tests in C#.NET

Paint QR Code JIS X 0510 in C#.NET Writing readable tests

Writing readable tests
Draw QR In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Recognizer In C#.NET
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Also notice that this test has no mock objects, only stubs. The assert is done against a return value, and a stub is used internally to simulate some scenario. This is often the way I like to write my tests. In fact, less than 10 percent of the tests I write have any mock objects. Most tests will have stubs, and nothing more.
Drawing Barcode In Visual C#.NET
Using Barcode creation for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
Linear 1D Barcode Printer In C#
Using Barcode generator for VS .NET Control to generate, create Linear 1D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
One more way developers tend to overspecify their tests is the overuse of assumptions.
Printing Code 128 In Visual C#
Using Barcode creation for VS .NET Control to generate, create ANSI/AIM Code 128 image in VS .NET applications.
www.OnBarcode.com
Draw EAN13 In C#.NET
Using Barcode creation for VS .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
www.OnBarcode.com
Assuming an order or exact match when it s not needed
Code 3/9 Generator In C#
Using Barcode generation for .NET framework Control to generate, create Code 39 Full ASCII image in Visual Studio .NET applications.
www.OnBarcode.com
I-2/5 Printer In C#.NET
Using Barcode generator for .NET framework Control to generate, create ITF image in .NET applications.
www.OnBarcode.com
Another common pattern people tend to repeat is to have asserts against hardcoded strings in the unit s return value or properties, when only a specific part of a string is necessary. Ask yourself, Can I use string.Contains() rather than string.Equals() The same goes for collections and lists. It s much better to make sure a collection contains an expected item than to assert that the item is in a specific place in a collection (unless that s specifically what is expected). By making these kinds of small adjustments, you can guarantee that, as long as the string or collection contains what is expected, the test will pass. Even if the implementation or order of the string or collection changes, you won t have to go back and change every little character you add to a string. Now let s cover the third and final pillar of good unit tests: readability.
Drawing QR-Code In Java
Using Barcode creator for Android Control to generate, create Quick Response Code image in Android applications.
www.OnBarcode.com
QR Code Generation In Java
Using Barcode maker for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
7.3 Writing readable tests
Making EAN 128 In None
Using Barcode creator for Office Word Control to generate, create UCC-128 image in Office Word applications.
www.OnBarcode.com
Barcode Encoder In .NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Readability is so important that, without it, the tests we write are almost meaningless. From giving good names to the tests to having good assert messages, readability is the connecting thread between the person who wrote the test and the poor soul who has to read it a few months later. Tests are stories we tell the next generation of programmers on a project. They allow a developer to see exactly what an application is made of and where it started. This section is all about making sure the developers who come after you will be able to maintain the production code and the tests that you
QR-Code Recognizer In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Paint Barcode In Objective-C
Using Barcode creator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
The pillars of good tests
Barcode Generator In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Reading Barcode In Java
Using Barcode Control SDK for BIRT reports Control to generate, create, read, scan barcode image in Eclipse BIRT applications.
www.OnBarcode.com
write, while understanding what they re doing and where they should be doing it. There are several facets to readability:
Print EAN-13 Supplement 5 In Visual Studio .NET
Using Barcode creator for Reporting Service Control to generate, create EAN13 image in Reporting Service applications.
www.OnBarcode.com
EAN13 Maker In None
Using Barcode printer for Excel Control to generate, create EAN13 image in Microsoft Excel applications.
www.OnBarcode.com
Naming unit tests Naming variables Creating good assert messages Separating asserts from actions
UPCA Maker In None
Using Barcode creator for Font Control to generate, create UPC-A image in Font applications.
www.OnBarcode.com
GS1 - 12 Creation In None
Using Barcode drawer for Software Control to generate, create UPC Code image in Software applications.
www.OnBarcode.com
Let s go through these one by one.
7.3.1 Naming unit tests
Naming standards are important because they give us comfortable rules and templates that outline what we should explain about the test. The test name has three parts:
The name of the method being tested This
is essential, so that you can easily see where the tested logic is. Having this as the first part of the test name allows easy navigation and as-you-type intellisense (if your IDE supports it) in the test class.
The scenario under which it s being tested This part gives us the with part of the name: When I call method X with a null value, then it should do Y.
part specifies in plain English what the method should do or return, or how it should behave, based on the current scenario: When I call method X with a null value, then it should do Y. Removing even one of these parts from a test name can cause the reader of the test to wonder what is going on, and to start reading the test code. Our main goal is to release the next developer from the burden of reading the test code in order to understand what the test is testing.
The expected behavior when the scenario is invoked This
A common way to write these three parts of the test name is to separate them with underscores, like this: MethodUnderTest_Scenario_Behavior(). Listing 7.23 shows a test that uses this naming convention.
Writing readable tests
Listing 7.23 A test with three parts in its name
[Test] public void AnalyzeFile_FileWith3LinesAndFileProvider_ReadsFileUsingProvider() { //... }
The method in listing 7.23 tests the AnalyzeFile method, giving it a file with three lines and a file-reading provider, and expects it to use the provider to read the file. If developers stick to this naming convention, it will be easy for other developers to jump in and understand tests.
Copyright © OnBarcode.com . All rights reserved.