Friday, November 30, 2012

Test Pattern, Given

Test Pattern
The Idea was to simplify validation of dependent functionality and is really useful if your already following the Exception over Assert pattern.
The concept is to use the similar comparisons to Assert to reduce the amount of code written in validation. I didn't want to use Assert for this because any failed assertion will result in a failed test.

Below I have two tests that do the same thing, change a users name in a system.  If either test cannot login as the user the test will error (not fail), and if either test cannot change the users name the test will fail. In the first test we check the return value of the LoginAs() method and if its not true we throw a new exception. Int he second test we Given the same way we would use Assert and it validates the return value of  the LoginAs() method and it raises an exception if the condition doesn't pass. It's also quicker to write and easier to read.
Code:
[Test]
public void ChangeUserName_Exception()
{
    if(!LoginAs("Rick"))
        throw new Exception("Login failed as user");
    Assert.That(ChangeUserName("Rick Casady"));
}
 
[Test]
public void ChangeUserName_Given()
{
    Given.That(LoginAs("Rick"));
    Assert.That(ChangeUserName("Rick Casady"));
}

Here is a basic version of the Given class, it's good to have other comparison functions found in the Asset class like (NotNull(), AreEqual(), AreNotEqual(), ...)
Code:
public class Given
{
    static public void That(bool condition)
    {
        Given.That(condition, Is.True, null, null);
    }

    static public void That(object actual, IResolveConstraint expression, string message, params object[] args)
    {
        Constraint constraint = expression.Resolve();

        if (!constraint.Matches(actual))
        {
            MessageWriter writer = new TextMessageWriter(message, args);
            constraint.WriteMessageTo(writer);
            throw new Exception(writer.ToString());
        }
    }
}

Constraint and IResolveConstraint can be found in NUnit.Framework.Constraints

Functional Test Pattern, Exception over Assert

The Idea is that a test should only result in a fail status if the functionality being tested isn't working and if the dependent functionality inst working it should only result in a error status. You can achieve this by raising a exception when the dependent functionality isn't working and Asserting on the functionality being tested.

Code:
// if it's dependent functionality
if(!UserCreated("TestUser"))
    throw new Exception("User wasn't created");
// if it's functionality being tested
Assert.That(UserCreated("TestUser"));

Use Assertion as little as possible in a functional test,

In a test suite for users and roles you might have 2000 test cases, but you might have only 100 test that actually test creating a user and the rest of the tests will test other functionality above and beyond that. Those other tests will have will have pre-steps or setup that will add the users before the actual test.

  • Create user (dependent functionality)
  • Check property 
  • Login as user (dependent functionality)
  • Check that user is logged in
  • Change user name (functionality being tested)
  • Check new user name


Code:
[Test]
public void ChangeUserName()
{
    User responseUser = CreateUser("Rick");
    if(responseUser.Name != "Rick")
        throw new Exception("User has incorrect name");
    if(!LoginAs("Rick"))
        throw new Exception("Login failed as user");
    Assert.That(ChangeUserName("Rick Casady"));
}

Now when you get an NUnit test report and the create user functionality is broken your report will say 1999 tests errored and 1 test failed. If you gave your tests meaningful names you'll know exactly what broke. Your test name might be AddAUserWithDefultPermissions()

Exceptions should also be raised in any helper classes any time there is a test run that has no failures and has errors you'll know you need to write at least one new test.

Thursday, November 15, 2012

Oktober-fresh

Oktober-fresh, Dara got this Oktoderfest from Norther Brewer, they have an awesome web site and lots of recipe kits.

Brewing day, This beer takes like 3 months to make but i'm going to try and get in done as quickly as possible 

Bottling day, Joe and Saeed were at my house helping me brew a stout so I enlisted there help to bottle it. They wont be around to bottle the stout so it it worked out they got to do this today.

Opening day, Nice malty flavor and a good kick to it. I really thought people at work wouldn't like this beer but everyone seemed to really like it. 


Stats
Style: Oktoberfest
Brew date:
OG: 1.055
Bottle date: 10/22/2012
Open date: 
FG: 1.014
ABV: 5.5%

Tuesday, November 13, 2012

Stout's Honor

Stout's Honor, was bought from Fermentation Solutions they called it "Seduction" it is an american stout.

Brewing day, I made this beer with a couple Medrio coworkers Joe and Saeed, it was there first time ever brewing so I tried to be as informative as possible.

Bottling day, we didn't hit the mark for the final gravity but it will still be a strong drink. I put most of the beer in 1 litters so I could easily share it with the boys.

Opening day, everyone loved the rich coffee/chocolate taste, we decided on a name for it "Stout's Honor" I think Joe picked the name.

Stats
Style: Stout
Brew date: 10/22/2012
OG: 1.068
Bottle date:
Open date: 11/8/2012
FG: 1.022
ABV: 6.1 %




Concentration Cider

Concentration Cider

Brew day, we bought 6 cans of apple juice concentrate and a  Cider yeast then did 10 minuets of Googleing and came up with this. It produced 2 gallons and I fermented for one week not expecting it to be extremely hard.

Bottling day, to charge the bottles I just used one can of apple juice concentrate, it was a lot more sugar than it needed so I had to check them frequently. The reason for so much concentrate I wanted it to be sweet.

Opening day, its very sweet and low in alcohol, very drinkable but you need a bunch to get a buzz. It still ad a little yeast smell when first opened, but longer we leave them in the bottles the less the smell. Next time well ferment it longer and use less sugar on bottling day

Stats
Style: Cider
Brew date: 7/15/2012
OG: 1.058
Bottle date: 10/20/2012
Open date: 11/10/2012
FG: 1.036
Alcohol By Volume: 2.89%


Double Honey Double

Double Honey Double, Dara got the recipe kit from Fermentation Solutions in Campbell but she had the flu so she sleep through the brew day.

Brewing day, I made this one with some Medrio coworkers Saeed, Joe, Sravan and +Saeed Gatson  and his girlfriend Cherie. We didn't hit the starting gravity number of 1066.

I left it too long in the bottles before refrigeration it and so I think we may have over carbonated it because it is a little bubbly.

Stats
Style: Belgian Honey Ale
Brew date: 11/10/2012
OG: 1.060
Bottle date:
Open date: 1/9/2013
FG: 1.018
ABV: 6.72