Skip to main content

Posts

Showing posts with the label TDD

TDD, where did it all go wrong?

Every year, a new generation of developers start their career. And every year, I see these new developers struggling with writing tests. And if you dare to go one step further, and introduce them to Test Driven Development, they get even more confused. Don't misunderstand me, they are quite fast in understanding the mechanics of the testing frameworks(MsTest, Xunit, ...). But it turns out that is really hard to write good, maintainable tests that help to move forward instead of becoming a bottleneck. One of the reasons this is such a problem, is that what is now described as Test Driven Development is not aligned with the original intention. Ian Cooper has a great talk exactly about this issue: Ian Cooper reminds what was Kent's original proposition on TDD, what misunderstandings occurred along the way and suggests a better approach to TDD, one that supports development rather impeding it. There are 2 key elements of his talk, that I think are most important: ...

SpecFlow Reusable steps

I’m a big fan of SpecFlow , a BDD style testing framework for .NET. For me it was a life changer, that made testing fun again. As you start to use SpecFlow more and more, you’ll see the same test steps appear over and over again. The guys from Endjin recognized the same pattern and bundled some of these steps in a re-usable NuGet package (together with some other useful functionality) . Here is the list of features from the GitHub site : Path & ScenarioContext extensions Shared Steps for dealing with Exceptions: [Then(@"an ""(.*)"" should be thrown")] [Then(@"a ""(.*)"" should be thrown")]       And Results:       [Then(@"the result count should be (.*)")] [Then(@"the result should equal the datetime (.*)")] [Then(@"the result should equal the datetimeoffset (.*)")] [Then(@"the result should equal the integer (.*)")] [Then(@"the result shou...

Unit Testing Examples

If you tried to write Unit Tests before, you know it can be hard. If you are looking for a little guidance, check the Unit Testing Examples and Labs on Github created by Dan Hellem. It covers the very basics to more in-depth materials such as dependency injection, mocking, and TDD. Here is the full list of covered topics: MSTest Unit Testing Basics XUnit Mocking Mocking Frameworks Dependency Injection TDD Data Driven Tests SSDT (Testing Stored Procedures) Exceptions Testing your JavaScript Jasmine Web Api Rest Services

Sharpen your TDD skills: Coding Katas

Looking for some good exercises to train your Test Driven Development skills(or train your programming language skills)? On https://github.com/garora/TDD-Katas you find a list with some of the most well known Coding Katas, like the Bowling Game kata, the FizzBuzz kata, and so on… For each of these Katas a solution is available in C# together with all the test. Certainly worth checking out!

Using different dataloaders in Effort

Last week I blogged about Effort , a library that helps you write unit test against your Entity Framework code. In that post I showed you how to populate the Effort in-memory database by using CSV files. Turns out that that is not the only way to populate the Effort database. Effort provides multiple built-in data loaders: EntityDataLoader: fetch data from an existing database by utilizing an existing Entity Framework compatible ADO.NET provider. CsvDataLoader: read data records from CSV files CacheDataLoader: speed up the initialization process by wrapping any kind of data loader with a cache layer And of course it is also possible to build your own data loader. More information can be found here .

Clean code cheat sheet

Looking for some principles, patterns, smells and guidelines for: clean code class and package design TDD – Test Driven Development ATDD – Acceptance Test Driven Development Continuous Integration ? Urs Enzler created the a new version of the Clean Code Cheat sheet , a short and useful list of DOs and DONTs for every professional software developer.

NUnit Test Adapter for Visual Studio 2012 updated

Starting from Visual Studio 2012 the testing infrastructure became extensible making it possible to add support for other test frameworks without the need of 3th party plugins like Resharper or Gallio . Just before the year ended, the NUnit team released a new version(beta 4) of their NUnit Test Adapter for Visual Studio, with support for VS 2012 Update 1. The new release, version 0.94, works with the VS 2012 RTM as well but shows off some extra features when used with the update. The biggest addition to the Adapter in this release is that it now supports Traits . NUnit Properties and Categories are translated in to Visual Studio traits and you can sort the display of tests by trait or filter on them. This goes a long way toward making the Visual Studio Test Explorer useful in running large sets of tests. Release notes for this version can be found here: http://nunit.org/index.php?p=vsTestAdapterReleaseNotes&r=2.6.2 More information about Traits here: http://blogs.msdn.com...

Microsoft adds Mocking/Isolation support to Visual Studio 11

A lot has changed on the testing front inside Visual Studio 11.(If you want to know a good overview of all the changes: http://www.peterprovost.org/blog/post/Whats-New-in-Visual-Studio-11-Beta-Unit-Testing.aspx ). One interesting feature I noticed in this long list of changes is the introduction of their own isolation framework: The Visual Studio Fakes Framework “Microsoft Fakes is an isolation framework for creating delegate-based test stubs and shims in .NET Framework applications. The Fakes framework can be used to shim any .NET method, including non-virtual and static methods in sealed types.” Wow! As it also will support to fake both non-virtual and static methods, I can finally isolate some of the harder to (unit)test parts of the .NET ecosystem. The Fakes framework helps developers create, maintain, and inject dummy implementations in their unit tests. It provides two ways to do this: Stub types Stub types make it easy to test code that consumes interfaces or ...

RhinoMocks: execute a specific action when a method is called

When writing unit tests sooner or later you are confronted with mocking . I always try to keep my mocking code as simple as possible. Sometimes this means creating a mock object manually and sometimes it means using a mocking framework. The mocking framework I use the most is RhinoMocks created by Ayende Rahien . Last week I had to mock a call to a specific method and execute some code when this method is called to test some specific behavior. Creating a mock object myself for this would be rather cumbersome, with Rhinomocks this becomes really simple: 1: var repository=MockRepository.GenerateMock<IDeliveryRepository>(); 2: repository.Expect(rep => rep.ReadDeliveryData( null , null )) 3: .IgnoreArguments() 4: .Do( new Action(()=>task.Process(data, delivery))); 5:   It’s important that the code you execute in the Do() matches the signature of the method that is called in the Expect(). The first time I impl...

Programmatically invoke an event handler for a control

To test the behavior of a custom Windows Forms button control I had created, I had to trigger the Click event handler from code. There are multiple ways to achieve this, but I choose to combine a helper class with some convention based reflection. I created a static class UIHelper with the following method: 1: public static void TriggerEvent(Object targetObject, string eventName, EventArgs e) 2: { 3: String methodName = "On" + eventName; 4: 5: MethodInfo mi = targetObject.GetType().GetMethod( 6: methodName, 7: BindingFlags.Instance | BindingFlags.NonPublic); 8: 9: if (mi == null ) 10: throw new ArgumentException( "Cannot find event named " +methodName); 11: 12: mi.Invoke(targetObject, new object [] { e }); 13: } In this code I took advantage of the fact that by convention event handlers are internally called by a protected method called On[EventName]. An example: ...

Clean Code Cheat Sheet

Writing code is hard, writing clean code is even harder. I found 2 cheat sheets about clean code. The first covers clean code – code that is easy readable and keeps changeable. The second is about Test Driven Development. Both cheat sheets list principles, patterns, practices and smells. Download them here : Clean Code , Clean TDD .

Run Tests in Current Context fails in Visual Studio 2010 RC

One very annoying issue I had after upgrading from Visual Studio 2010 Beta 2 to the RC, that the ‘Run Tests in Current Context’ options runs all tests in my solution. As I’m having a lot of tests, I needed a solution. It turns out that this is caused by a change that was made in Visual Studio 2010 RC to the way Test Settings files are stored. In this blog post , they describe that you can fix the issue by opening each Test Settings file (*.testsettings) contained in your solution, making a trivial change, and saving it.

NDbUnit v1.6 supports Oracle

A new version of NDbUnit was released and Oracle support has finally been integrated. This now brings the complete list of supported DB targets under NDbUnit to six: Microsoft SQL Server (including Express Editions and on up to Enterprise) Microsoft OleDb -supported databases* Microsoft SqlServerCe /Mobile Edition/whatever its called this month MySql Oracle (including XE and on up to Enterprise) SqlLite (including both on-disk and in-memory variants)

Complex bugs and multithreading

If you are using unit test, you should know that every test is executed on it’s own thread. Last week I had a strange error where two tests failed(sometimes). Both tests were using an EventWaitHandle to do some synchronizationruns. If I both tests by themselves, the test run was successful. However, if ran them together, then I got the following error: Test method threw exception: System.ObjectDisposedException: Safe handle has been closed. I found out that the two tests were sharing an EventHandler. The first test raises an event and dispose itself. But in my code I never unattached the event handler . So when the second test raises the event, the eventhandler still thinks that there are 2 methods to call, not just one. After changing the code to properly detach the event, everything was working. So unittests are also a good test to check if your code is thread-safe.

NDbUnit- Invalid object name

For our integration testing, we are using NDbUnit , a free open source .NET library for managing database state during testing. It ensures that our database is always in a consistent state before we start the execution of our tests. It uses an XSD schema to describe the database tables and an XML file can be used to provide the content. After configuring everything, we initialized our database using following code 1: var mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest( @"server=;user=;password=;initial catalog=" ); 2: mySqlDatabase.ReadXmlSchema( @"./Test.xsd" ); 3: mySqlDatabase.ReadXml( "./Data.xml" ); 4: mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity); But instead of populating our database with the correct values, we got an Invalid Object Name exception for each table. We found out that NDbUnit uses the default schema of the user specified in the connection string to look for the tables. So ...

TechDays 2010 Belgium

Ive been confirmed for a session at TechDays 2010 Belgium( www.techdays.be ). I’ll be talking about: Making TDD work using Visual Studio 2010 Visual Studio 2010 brings with it several enhancements to help cut development friction and enable the developer to focus on the task at hand: writing high-quality code. Join Bart in a session where he dives deep in Visual Studio 2010 to show the improvements being made to better enable test-driven development. Obviously this session could not be complete with a proper introduction about Test Driven Development, introduction of mocks, principles of testable code and a quick peek into Behavior Driven Development(BDD). So… get ready for a joyful test experience. So if you are interested in how you, as a developer can write better, more testable code , come and listen. You ain’t gonna regret it ;-)

TDD: Test Driven Development or Test Driven Disaster?

Inside our organization we have a discussion evening tonight about TDD with the promising title 'TDD: Test Driven Development or Test Driven Disaster?'. I hope it will be an interesting discussion. Searching for some extra arguments for the 'Pro' camp, I stumbled on this blog post: TDD is not about testing, it's about how we develop software . I cannot agree more with the statements mentioned in this post.