Products

Solutions

Resources

Partners

Community

About

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.


Creating Testable Modules - Introduction

At this years Open Force Europe conference, I will be doing  a presentation on Testable Modules.  In preparation for that talk, I have been researching and developing examples.  In this series of blogs I will be building the concepts for creating Testable Modules.  My goal here is to show you how to adapt your Module Development process to make your modules more testable.

Phil Beadle has developed an excellent project template for Test-Driven Module Devlopment, and if you are interested in this topic you should review his posts and download his template.  I propose to take a slightly different tack, and show you how (and why) each decision in the design process is made. 

I would like to emphasise that I am new to this game - I will be writing these blogs as I learn and develop the ideas - so if there are any purists out there reading this - please accept my apologies for any egregious errors, or over-simplifications.

In this first post of the series (which is cross-posted from my personal Blog), I provide some background information to get us started.

Creatting Testable Modules - Part 1, Introduction

In a traditional ASP.NET Application, a page (WebForm) contains controls (WebControls and UserControls) that display application domain data. A user can modify the data and submit the changes, via Postbacks. The page retrieves the domain data, handles user events, alters other controls on the page in response to the events, and submits the changed domain data to the Business Layer.

Including the code that performs these functions in the Web page makes the class complex, difficult to maintain, and hard to test. In addition, it is difficult to share code between Web pages that require the same behavior.

Recently there has been much more emphasis on the use of automated Unit tests in the development cycle.  The benefit of automating Unit Tests, which can then be run whenever a change is made to the code is that it has the potential to free the QA resources to focus on the Usability of an application and on the exceptions (edge cases), as the application has been well tested before it gets to the QA team. 

Of course, this all depends on how well the tests have been written and how much of the Application the tests cover (code coverage).

Unit Tests

A unit is the smallest testable part of an application. Typically, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class.

Ideally, each unit test is independent from the others, and double objects like stubs, mock or fake objects as well as test harnesses can be used to assist testing a module in isolation.

The goal of unit testing is to isolate each section of the program and show that the individual sections are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits, as unit tests find problems early in the development cycle.

Test Driven Development

Test-Driven Development is a style of software development that takes this use of testing to the extreme.  In Test-Driven Development (TDD) the Unit Test is written first, and then the minimum amount of code is written to get the test to pass.  Most testing frameworks use the red light/green light approach – red light, the test fails – green light, the test passes.  The complete cycle of Test Driven Development is as follows (from Test-Driven Development, By Example – Kent Beck)

  1. Write a test
  2. Run all tests – and see the new test fail
  3. Make a little change – to make the test pass
  4. Run all tests and see them all succeed
  5. Refactor to remove duplication
  6. Repeat

The important steps are (3) and (5).  The initial coding to make the test pass should be as small as possible. 

For instance if you are creating a math function to multiply two numbers together and your test invloves calling the function with the two numbers 2 and 5, and checking that the answer is 10, a TDD purist would say that the initial implementation of the function should just return the literal constant integer 10, as this is the simplest code that satisfies the test. 

It is in step (5) when the duplication is removed that causes the code to be refactored to return param1 * param2.

Loosely Coupled Systems

In order to test one component, independently from another, the components should be loosely coupled.  This is not always the case with software.  Often a business layer class will have a dependency on the database.  In this scenario, a method in the business layer cannot be executed without affecting the database.  The data layer can probably be independently unit tested, but the business layer is said to be tightly couple to the data layer, as it cannot exist in isolation.

Testable DotNetNuke Modules

The conventional methodology for building DotNetNuke modules does not lend itself to support Unit Testing (or by extension Test-Driven Development) for a number of reasons.

  1. While the Data Layer is quite loosely coupled from the Business Layer (through the DataProvider model) the providers are configured through the web.config file, and there is no way (prior to version 5.0) to independently create a MockDataProvider to enable us to test the Controller classes in isolation from the database.
  2. Web Forms (and User Controls) do not provide a way to independently Unit Test the Presentation Layer – there are browser-automation frameworks like Watin and WebAii, which can automate user interaction with the Page – but these are not Unit Tests.

In this series of blog posts I will discus ways we can modify our normal module development process to create Testable Modules.  While I will discus Test-Driven Development, the real goal in this series of blogs is to build testable modules, regardless of the development process you decide to use.

 

Comments

Comment Form

Only registered users may post comments.

NewsArchives


Aderson Oliveira (22)
Alec Whittington (11)
Alessandra Daniels (3)
Alex Shirley (10)
Andrew Hoefling (3)
Andrew Nurse (30)
Andy Tryba (1)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (37)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Bogdan Litescu (1)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (213)
Chris Paterra (55)
Clint Patterson (108)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (181)
Daniel Valadas (48)
Dave Buckner (2)
David Poindexter (12)
David Rodriguez (3)
Dennis Shiao (1)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (80)
Francisco Perez Andres (17)
Geoff Barlow (12)
George Alatrash (12)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (274)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Kelly Ford (4)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matt Rutledge (2)
Matthias Schlomann (16)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Miguel Gatmaytan (3)
Mike Horton (19)
Mitchel Sellers (40)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Oliver Hine (1)
Patricio F. Salinas (1)
Patrick Ryan (1)
Peter Donker (54)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Sacha Trauwaen (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott Schlesier (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Steven Fisher (1)
Tony Henrich (3)
Torsten Weggen (3)
Tycho de Waard (4)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (40)
Will Strohl (180)
William Severance (5)
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out