Archive for the ‘Docs & Guides’ Category

Unit tests: expose bugs and spend less time fixing them!

Hands-on introduction to creating unit tests that cover your application’s business logic

An extra effort and more code: two major drawbacks that often discourage developers from writing unit tests. They seem hard to write and little to contribute or at least that is what I first experienced when I started using them. And although I still don’t consider myself to be an expert in the field of test-driven development, I have not given up and can say: unit tests are a real time saver. Rather than walking through a long list of benefits, let’s write some code and see for ourselves..

Before you can write a unit test you obviously need to have a chunk of code or some business logic that requires testing. The following example shows you how to add a unit test after writing your initial code and how you can alter the unit test before writing any additional code (e.g. in a second iteration).

Important to note: although the example is created in C# with the Visual Studio unit-testing framework, the principles can be applied to any given library in your preferred programming language.

The full source code of the example can be downloaded here.

Example: Testing the shopping basket of a web shop
Let’s say you are working on a web shop and you are asked to implement some logic that calculates the total price of a shopping basket. You’ve created the shopping basket and price calculation logic in the business tier:

public class WebshopFacade
{
public void AddProductToBasket(ShoppingBasket basket,
Product product)
{
basket.Add(product);
UpdateBasketPrice(basket);
}

public void UpdateBasketPrice(ShoppingBasket basket)
{
double totalPrice = 0;
foreach (ShoppingBasketItem item in basket.GetItems())
{
totalPrice += item.Product.UnitPrice * item.Amount;
}
basket.TotalPrice = totalPrice;
}
}

The code is pretty straightforward: the web shop can create a basket but to add an item it needs to call the façade which will than update the basket’s total price. Now, before creating a web page (and I know you want to) it’s a good practice to set up a unit test. Why? If there are any coding errors in the shopping basket or its price calculation, you want to expose these bugs right now, and not by experiencing strange issues when creating the web page which will eventually have you debug the web application. The unit test may already save you from losing precious time, and surely you want to hold on to it for future testing when the price calculation logic becomes more complicated.

Test method 1: Initial price of the basket
Ok, let’s start out with a basic yet crucial test: you want to make sure that when the web shop creates a basket, the initial total price is zero. You may laugh, but your customers won’t if this is not the case!

[TestClass]
public class ShoppingBasketTest
{
public ShoppingBasketTest()
{
}

[TestMethod]
public void TotalPriceOfEmptyBasketMustBeZero()
{
ShoppingBasket basket = new ShoppingBasket();
Assert.AreEqual(0, basket.TotalPrice, "Expected total price to be zero for empty basket");
}
}

That wasn’t so hard, was it? Running all tests in Visual Studio takes only a few seconds and for me it resulted in a test run completed, results: 1/1 passed. Sweet, an instant confidence boost!

Test method 2: Adding products to the basket
Let’s move on and add a test method that ensures products can be added to the basket:

[TestMethod]
public void ProductsCanBeAddedToTheBasket()
{
ShoppingBasket basket = new ShoppingBasket();
WebshopFacade facade = new WebshopFacade();
MockFactory mocks = new MockFactory();

for (int i = 0; i < 50; i++)
{
facade.AddProductToBasket(basket, mocks.CreateProduct());
}

int totalUnits = 0;
foreach (ShoppingBasketItem item in basket.GetItems())
{
totalUnits += item.Amount;
}

Assert.AreEqual(50, totalUnits, "Expected 50 units in the basket after adding 50 random products");
}

In this test 50 random products are added to the basket, and then the test does a recount by looping through the basket’s items. As the test method cannot create products directly (nor can the web shop for that matter) a mock factory is used to create mock objects, in this case products.

And believe it or not, when I first ran the test it failed because of a bug in the basket. In Visual Studio you can add a breakpoint inside any test method and instantly debug the test method, so I was able to resolve the issue in no time!

Test method 3: Price calculation
Finally, you can add a test method which ensures the total price is calculated correctly:

[TestMethod]
public void BasketTotalPriceIsCalculatedCorrectly()
{
ShoppingBasket basket = new ShoppingBasket();
WebshopFacade facade = new WebshopFacade();
MockFactory mocks = new MockFactory();

double expectedTotal = 0;
for (int i = 0; i < 50; i++)
{
var product = mocks.CreateProduct();
facade.AddProductToBasket(basket, mocks.CreateProduct());
expectedTotal += product.UnitPrice;
}

Assert.AreEqual(Math.Round(expectedTotal, 2),  Math.Round(basket.TotalPrice, 2), "Expected total price does not match basket's total price");
}

The code is mostly copied and pasted from the previous method, but here the price calculation is somewhat mimicked. It may seem obvious that this test will pass, but when you make further progress in developing the basket, this test method can fail, and it will expose any bugs that were introduced by your (or someone else’s) changes.

Another way of testing this logic would be to create some specific products, and to hard code the expected total price. This could include special situations that don’t frequently occur but are part of the business logic, e.g. discounts for certain products, discounts for a specific amount of products, etc.

Creating a unit test before writing any code
The unit test now covers the basic functionality of the basket and you might decide to start a second iteration in which you introduce removal of products or discount logic. In either case you can initiate this iteration by creating new unit tests or updating the existing one allowing you to focus on the requirements before writing any code.

You can start by changing the “interface” of the façade, by adding a method for product removal:

public void RemoveFromBasket(ShoppingBasket basket,
Product product)
{
throw new NotImplementedException();
}

And now you can add new test methods that validate the product removal logic:

[TestMethod]
public void ProductsCanBeRemovedFromTheBasket()
{
ShoppingBasket basket = new ShoppingBasket();
WebshopFacade facade = new WebshopFacade();
MockFactory mocks = new MockFactory();

var addedProducts = new List
();
for (int i = 0; i < 50; i++)
{
var product = mocks.CreateProduct();
facade.AddProductToBasket(basket, product);
addedProducts.Add(product);
}

for (int i = 0; i < 25; i++)
{
var remove = addedProducts[i];
facade.RemoveFromBasket(basket, remove);
addedProducts.Remove(remove);
}

int totalUnits = 0;
foreach (ShoppingBasketItem item in basket.GetItems())
{
totalUnits += item.Amount;
}

Assert.AreEqual(25, totalUnits, "Expected the basket to have 25 remaining units after removal");
}
[TestMethod]
[ExpectedException(typeof(ShoppingBasketException), "Expected shopping basket exception to be thrown")]
public void ProductMustExistInBasketOnRemoval()
{
ShoppingBasket basket = new ShoppingBasket();
WebshopFacade facade = new WebshopFacade();
MockFactory mocks = new MockFactory();

facade.RemoveFromBasket(basket, mocks.CreateProduct());
}

Obviously the unit test will fail if you run it before you have written the actual implementation, but for sure you will gain a great deal of insight in how to set up any interfaces, how objects should behave or interact, how exceptions are thrown, etc.

Try it out yourself!
A demo, including the example’s full source code can be downloaded here. You can run the tests in Visual Studio by pressing CTRL-R A or through the menu as shown in the screenshot below. If you are new to unit testing and can use a little practice, I strongly recommend that you try and write the code for product removal, and see if you can make the failing unit test pass again.

Final considerations
• Any unit tests that you create are there to stay and the more complex a module becomes the more often you will find that bugs are exposed when the module is changed or re-factored, simply by rerunning the tests
• By creating advanced unit tests you are to mimic and validate every possible in- and output of a certain module, allowing you to immediately recognize and debug the most specific / exotic situations when a test fails
• Unit tests may seem like a lot of code to write, but most of the time you are just writing the consuming code that has to be implemented anyway
• Unit tests offer great insight in how a module behaves or is to be used. Developers that are not familiar with the code can walk through the tests as it were code documentation

Hope this helps!

An introduction to regular expressions

Wikipedia describes Regular Expressions like this: “In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters.”. That’s almost as clear as a fresh glass of one of the finest Belgian beers :) In this post I’ll try my best to get you familiarized with the basics of Regular Expressions.

What?

Regular Expressions (regex or regexp) are a very powerful “tool” that’s available is most common programming languages (PHP, Actionscript, Javascript, C#, …) but can be quite complex to grasp. Simply put: It’s a unified way of finding or replacing complex parts of a string. Well, it can be much more complex than that, but I guess this is the most common reason why we use regex.

How?

Some smart bloke just decided to use patterns to define Regular Expressions. These patterns are predefined and you just got to learn them by heart (yeah, of course… there are tools available, more on that later). Some pattern examples:

\d searches for a digit
[a-z] searches for a single character in the range from a to z
(a|b) searches for the string “a” OR the “b”
a{2,4} searches for at least 2 “a” characters next to each other and maximum 4 (for example: “a” and “aaaaa” won’t match, but “aa”, “aaa” and “aaaa” would)

E-mail validation

Although the examples above are quite clear, the whole concept of Regular Expressions might still seem a little vague to you. So to fully explain regex I think it’s better to give you a real life example of probably the most common use of regex: e-mail validation. Emails addresses always have a similar structure: <part.one>@<part.two>.<tld> (I deliberately used a point in the first two parts because it can contain a point, but it might as well can contain a dash or an underscore). This could be a good example of a regex for e-mail validation:

([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+

If you’re new to regex you might find the code above pretty unreadable, but I think it’s probably the most readable version of a regex for e-mail validation. :) This one is not:

/^\S+@\S+\.\S+$/

Now you know that there always are multiple solutions to get the same effect, but don’t let that scare you off, let’s go trough the first example step by step:

  1. ([a-zA-Z0-9])+ will match any character in the range from a to z and A to Z (case sensitive) and 0 to 9. Because of the + after the brackets it will fetch multiple chars (unlimited number).
  2. ([a-zA-Z0-9_\.\-])+ to allow an underscore, point and dash, you just add these characters (notice the backslash, this is used because a point and dash have a predefined regex meaning too).
  3. @ this is the actual @ character from the e-mail address :)
  4. (([a-zA-Z0-9\-])+\.)+ same here, the a to z, A to Z and 0 to 9 allow all chars in that range plus the dash and at the end a point. Notice the plus at the end, with this the previous group can be matched multiple times.
  5. ([a-zA-Z0-9]{2,4})+ the last bit, same story a to z, A to Z and 0 to 9 but in stead of unlimited chars it’s limited by 2, 3 or 4. That’s done with the curly brackets.

Now you know how it’s done, but it’s going to take a while to get used to and even longer to write them yourself. When you are making Regular Expressions, you often look at the result and are almost amazed that you wrote them yourself, because they look so complex when they are done.

The idea of regex is simple, but you have to write them one step at the time. That’s the same when you are learning regex: one step at the time. First you’ll be reading them, understanding them, making some modifications of your own, until you end up writing them yourself. It’s very important that you know exactly what everything does, for example the curly braces (to limit the previous found element) or the dash (used to find a range of chars). To learn the pattern syntax, check out this page or Google for regex examples.

Tools

There are numerous tools available to make Regular expressions. One of the best is this RegExr app by gskinner. He made an Adobe AIR desktop app out of it too.

Real life examples (Actionscript, Javascript and PHP)

As I said before, there are always multiple solutions for making a regex. Below some real-life examples on how to use e-mail validating Regular Expressions in AS3, JS and PHP:

AS3

private function validMail(mail:String):Boolean {
    var emailExpression:RegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i;
    return emailExpression.test(mail);
}

Javascript version:

function isEmail(v) {
    return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(v);
}

PHP version:

function isEmail($email) {
    $regex = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+/';
    preg_match($regex, $email, $isEmail);
    return (bool) !empty($isEmail);
}

The Modulo Saves Lives… and Time!

Today’s blog post features one of my favorite operators: the modulo (%). The modulo returns the remainder of the division of one number by another. So for instance if you divide 4 by 2, you will get zero. If you divide 4 by 3, you will get 1, and so on.

Below is a case in which I often use the modulo operator. It’s basically a row counter, most commonly used when you’re working with a grid. The code at the bottom, at least in my opinion, is much cleaner and above all, shorter, than the code above it.


$counter = 0;
$total = 4;

// Good code

if($counter &amp;amp;amp;amp;lt; $total){
$counter++;
}else{
$counter = 0;
}

// BETTER code...

$counter++;
$counter = $counter % $total;

Reference guides ++

More:

and many more on Cheat sheets.org.

Reference guides

The people at veign.com have created several reference guides that give a quick and handy reference on the standards that will be the foundation for all websites. Many thx to Veign and let’s print all of them and hang them above our bed like real geeks should do :)