Monday, January 23, 2012

AgileDotNet 2012

My company, Improving, is hosting our third annual AgileDotNet conference in Dallas on Friday, February 17th. It will be a day of presentations and discussion focused on agile software development on the Microsoft stack, ALM tools, and the leadership and cultural issues involved in agile IT.  Hope you can  join us!

Full details can be found here.

Monday, December 26, 2011

Using Gists for Code Snippets

Normally, I use Windows LiveWriter to write my blogs at BlogSpot.  And I have always used syntax highlighter plug-ins to insert code snippets.  These utilities in the past inserted a lot of html elements and style definitions in-line with the rest of the post or required setting up global stylesheets and javascript in order to work.

In the last few weeks, as I have been looking at transitioning my blog to a ruby-based engine called OctoPress, I found another mechanism for inserting code snippets - gists from GitHub.  It allows you to store your code snippets online and easily add them to your posts with one line of code.

All you need to do is:

1.  Go to http://gists.github.com

2.  Sign up for  a free GitHub account

All you have to do is click on the link in the upper, right-hand corner "Sign up for a GitHub account".  It leads you through a process.


If you don't wish to create an account, you can create gists anonymously as well.

3.  Add gist to GitHub

All you need to do in order to add a new gist is: 

a.  Create a name for the gist
b.  Create a name for the file (this is optional)
c.  Select a language 

If you give the file a name, the system will determine what the language is from the extension.

d.  Put the text of the code snippet in the body of the gist
e.  Save the gist by clicking on the Save Gist button.


4.  Add the gist in your post

In order to add the gist to your post, you will need to click on the show embed link and add the html snippet to the html content of your post.  In this case, 

<script src="https://gist.github.com/1522062.js"> </script>

Once you have added this, the code should appear in the post as below.



5.  Add line numbers via additional css styles

You may notice once using the steps above that the native gists from GitHub do not support line numbers currently, so in order to add them I found a gist that contains the .css styles that you need to add to your blog site.  Once added, all snippets appear with line numbers as you saw in point number 4. 



If you find another way to do this by adjusting settings at GitHub, let me know in the comments.

Hope this helps!

Friday, February 11, 2011

Creating Data-Driven Tests in MS Test

For my current client, I was looking for a way to create a data-driven unit test in MSTest.  Basically, you can define a test once and then define a data source that provides multiple rows of data to be sent through the same test.  That way, you don’t have to write redundant tests.
I had done this previously, but I couldn’t remember how it was done.  I’m writing this post in order to remind myself and hopefully help someone out there.
1.  Create a Test Method
The first step is to make sure you have a test method.  You can do this by making sure that in your test project you have at least one class decorated with the [TestClass] attribute and a method with the [TestMethod] attribute. 
[TestMethod]
public void Add_when_two_numbers_returns_proper_result()
{
}



When creating this test class, make sure to keep the TestContext property as it is defaulted.  You will be using this when coding your test.


private TestContext _testContext;
 
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return _testContext;
}
set
{
_testContext = value;
}
}



2.  Add a Test Data File

For the purposes of this explanation we will be using a simple .csv file (TestData.csv).  Make sure to add a text file, preferably at the root of your project with that extension.  In the file, you will want to add data.  I have added three columns per row:  Operand1, Operand2, and ExpectedValue which represents the expected result of adding the two operands together.  You can add as many rows of data to check all of the boundary conditions that express your intentions for your unit test.

image

Right-click on the file and select Properties to set the Build Action to Content and the Copy to Output Directory to Copy Always as shown below.  This will ensure that the file is copied to the Bin folder when running your unit tests.

image

3.  Open the Test View from the Test menu.

From the Test menu, select the Windows –> Test View option as shown below.

image

Once you open the Test View, you should see a list of the test methods that are available.  When a test is highlighted, you will see the properties for that particular test in the Properties pane as shown below. 

image

4.  Assign Test File as Test Data Source

Find the row for Data Connection String in the Properties pane and click on the button to the far right with the ellipses (…).  That should open the following dialog where you will be presented with an option to set up a database, csv file, or xml file.

image

The database option should be self explanatory.  It is like any other database connection, and the most popular option is to use a simple MS Access DB or SQL Express database that you can include with your project.

The other two options are file based.  The CSV file will be explained in further detail below.  The main difference between a CSV and XML file is that you can have multiple test data sets in the XML file while only one set of data would be accessible for a CSV.  So, if you need to set up data for multiple tests, you would need to maintain multiple files with the CSV option.  In the XML option, you could just maintain one with multiple sections within the XML file.

Select the CSV File option and click Next.

5.  Select Your CSV File

At this point, you will be presented with a dialog to select a CSV file.  Click on the (…) button and select the location of the text file that you created for your test.

image

Once selected, the dialog will show the contents of your CSV file as a data grid to allow you to examine the contents to make sure that the data is what you were expecting.

image







Click the Finish button once you have confirmed the file contents.  You will notice that the properties have been updated to reflect the change.

image

In addition, Visual Studio will add a [DataSource] and [DeploymentItem] attributes to your original test method as shown below with the same information.  In the future, you can set these attributes up on a test without needing to use the Properties wizard for selecting a Data Source.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestData.csv", "TestData#csv", DataAccessMethod.Sequential), DeploymentItem("Demo.DataDrivenTests\\TestData.csv"), DeploymentItem("TestData.csv"), TestMethod]
public void Add_when_two_numbers_returns_proper_result()
{
}




6.  Add Code to Reference Data Source

Once you have added your test data to the test method, you will want to access it within the body of your unit test method.  This can be done by referencing the DataRow property of the TestContext class-level variable.  I have created a simple test to use the values to assert that the Add method of a simple Calculator class works as shown below.  Because a DataRow column returns an object, you will need to do some casting to the appropriate data types.

[DeploymentItem("Demo.DataDrivenTests\\TestData.csv"), DeploymentItem("TestData.csv"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestData.csv", "TestData#csv", DataAccessMethod.Sequential), TestMethod]
public void Add_when_two_numbers_returns_proper_result()
{
//arrange
var operand1 = System.Convert.ToInt32(TestContext.DataRow["Operand1"]);
var operand2 = System.Convert.ToInt32(TestContext.DataRow["Operand2"]);
var sut = new Calculator();
 
//act
var result = sut.Add(operand1, operand2);
 
//assert
var expectedValue = System.Convert.ToInt32(TestContext.DataRow["ExpectedValue"]);
Assert.AreEqual(expectedValue, result);
}



7.  Run the Test

You can run the test as you would any other in MS Test.  The result will look similar to other tests.

image

However, if you right-click on the result row in the Test Results pane, and select View Test Results Details as shown below…

image




…you will see the detail of the results to see which data combinations failed and why.

image

Hope this helps someone else out there.

Thursday, September 30, 2010

Create Custom Test Category Attributes in Visual Studio

In the past, for each class library project we create in Visual Studio, we have maintained separate unit test and integration test projects.  This has allowed us to easily configure continuous integration with TFS to run a given project’s unit tests on check-in and run the full suite of unit and integration tests on a nightly build.
With the introduction of Visual Studio 2010, build types now can filter tests based on test categories. 

For example, I can categorize a given test as a unit test by doing the following:

        [TestClass]
    public class Test
    {
        [TestMethod]
        [TestCategory("UnitTest")]
        public void IsValid_returns_false_when_quantity_large()
        {
        }
    }



This allows us to consolidate all tests into one project and distinguish by categories.  The only issue is that these categories are based on strings that the developer must type, and this can create inconsistencies using magic strings which may not be noticed during the course of development.

In order to get around this, another option is to create your own custom test category attributes that return the proper string.  Microsoft has provided a base class, TestCategoryBaseAttribute for this very purpose.

         public class UnitTestAttribute : TestCategoryBaseAttribute
    {
        public UnitTestAttribute()
        {}

        public override IList<string> TestCategories
        {
            get { return new List<string> {"UnitTest"}; }
        }
    }


By overriding the TestCategories property, you can return the appropriate string(s) that represent this classification.  In this case, we are returning the same string that the previous test category provided.

In order to use this attribute, you merely need to add it to a given test method like so.

        [TestClass]
    public class Test
    {
        [TestMethod]
        [UnitTest]
        public void IsValid_returns_false_when_quantity_large()
        {
        }
    }


With this kind of flexibility, if you had a limited list of attributes that you wanted to provide to developers, you could also create an enum type and pass that into the constructor of the custom test category attribute. 

Just create an enum type.

         public enum Category : int
    {
        UnitTest = 0,
        IntegrationTest = 1
    }


Then, create the attribute using the enum as a constructor parameter.

         public class TestCategoryAttribute : TestCategoryBaseAttribute
    {
        private TestCategory _category;

        
        public TestCategoryAttribute(TestCategory category)
        {
            _category = category;
        }

        
        public override IList<string> TestCategories
        {
            get
            {
                var value = Enum.GetName(typeof (TestCategory), _category);
                return new List<string>{ value };
            }
        }
    }



To use the new attribute, you would do the following.

        [TestClass]
    public class Test
    {
        [TestMethod]
        [TestCategory(Category.UnitTest)]
        public void IsValid_returns_false_when_quantity_large()
        {
        }
    }


Hope this helps!

Monday, August 23, 2010

Six Interview Questions for Agile Teams

I was reading through my blogs tonight and came across one that did a nice job of explaining the qualities that an agile team member should possess.  In addition, the writer included a good behavioral interview question for each.
I briefly share each below, but I highly recommend reading the original article.
1. People Who Collaborate
"Think back to a recent project. Give me an example of a time you had to work with other people to make sure that you could finish something. What happened?"
2. People Who Ask for Help
"Think back to your most recent project. Tell me about a time you did not understand something. What did you do?"
3. People Who are Willing to Take Small Steps and Get Feedback
"When you work on your projects outside of work, how do you work?"
4. People Who are Willing To Do Something That is Good Enough for Now
"Tell me about a recent time you did not know everything at the beginning of the project. What did you do?"
5. Adaptable People
"Tell me about a time when you did not have the conditions you would've liked for your project. What did you do?"
6. People Willing to Work Outside Their Expertise
"Tell me about a time you took on work to help the team. What was that like?"
So, tell me dear Reader – what questions do you ask when interviewing candidates for an agile-oriented team?

Saturday, May 29, 2010

Motivations

I found this video about what motivates people to be very refreshing.  Basically, if the tasks that people are working on require more than a basic physical action, money rewards don't motivate, but rather demotivate people.  What really motivates others to perform well at conceptual tasks are three things:  autonomy, self mastery, and purpose.  What I find is that Agile practices tend to foster or work within these motivations.  

For example, user stories really help to zero in on the purpose for a given feature request.  "As a [actor], I can [do something], so that I can [accomplish a goal]."  Developers really get a sense for why they are building a piece of functionality rather than just focusing on building a method, etc.  And the prioritization of these stories helps to clarify what is truly important within a sprint.

As well, agile practices promote mastery by emphasizing continual improvement.  Teams are encouraged to use the processes that work for an individual team rather than trying to create a process that will work for an entire organization.  Other principles such as refactoring and unit testing also suggest an intense focus on continually improving the code base.

And finally, the scrum meeting and agile planning in general focus on the team working together on deciding the priorities rather than having a central authority such as a PM do that.  Individuals make themselves accountable to the team by explaining what they accomplished yesterday and what they plan to accomplish for the current day.  This creates a sense of autonomy in those performing the work.

I would be interested to hear your thoughts on the video as well as whether or not you agree that agile practices help to foster these motivations. 

Sunday, November 29, 2009

Rick Warren Speaks on Meet the Press - Giving

I really enjoyed seeing Rick Warren, the pastor of Saddleback Church in California, speak on giving on Meet the Press with David Gregory. (below)

And what influence he has - not just because he has a platform on national television, but because he actually gives 90% of his income. He has the authority to speak on this subject!