Use Parameters from TFS Test Cases in NUnit Unit Tests

One of the things we have been doing a lot lately is getting our testers involved more in API method creation/development. With technologies like swagger this is becoming a lot easier to get non-developers involved at this level, and I find its great to have a “test driven” mind helping developers out with creating unit tests at a low level like this.

One of our problems though is that the testers have been coming up with test cases fine, but getting them into our code has been a mission.

Our testers know enough code to have a conversation about code, look at some basic loops and ifs, but not enough to edit it, so in some cases we’ve been having Chinese whisper issues in getting the cases into the code. Also we have had some complex test methods recently (10+ parameters, 30+ test cases) and storing these into C# code its hard to visualize when look at code and not something like a grid/table (excel, etc.).

For the later requirement we had considered CSV storage, and I have done some code for this in my library for use if you want it. But we decide to go with TFS test cases for storing the data, because i was working on updating the Test Cases from the NUnit tests anyway .

Ideally i wanted to do something like this:


 [Test]
 [TfsTestCaseDataSource(65871)]
 public void MyUnitTestMethod()
 {
 // Test stuff here
 }

And have it draw from the test case in TFS (example below).

TestCaseInTFSStoringDataToUseInUnitTestNUnit

However there is some issues with passing parameters to NUnit Tests that prevented me from doing this.

So instead what we have to do is create a class for each Unit Test we want to get its Data from a Test Case in TFS. This is the current work around. I may yet fix this in NUnit one weekend if I am not too hungover (but there is little change of a weekend like this).

Library I am using here is available on nuget.org

So using the library you have to do the following

1. Create a class for the data source that inherits from the Class in NUnitTfsTestCase.TestCaseData.TestCaseDataTfs Hard code your TFS test case ID in here


using System.Collections.Generic;
using NUnitTfsTestCase.TestCaseData;

namespace MyNamespace.MyFixture.MyMethod // use a good folder structure please 🙂
{
 class GetTestCaseData : TestCaseDataTfs
 {
 public static IEnumerable<dynamic> GetTestData()
 {
 return TestCaseDataTfs.GetTestDataInternal(65079);
 }
 }
}

2. Add an attribute to you test case that references this


[TestFixture]
class MyTestClass
{

[Test]
[Test, TestCaseSource(typeof(MyFixture.MyMethod.GetTestCaseData), "GetTestData")]
public void MyUnitTestMethod(strin someParam1, string someParam2)
{
// Test stuff here
}
}

3. Add app-settings for the TFS server and TFS project name


<add key="tpcUri" value="https://mytfsserver.com.au/tfs/DefaultCollection" />
<add key="teamProjectName" value="MyProjectName" />

Its important to note here that I am using a build agent that is on the same domain as an on-premise TFS server using a Domain account with appropriate permissions to the TFS server. I will work on example code in the near future for this hit VSTS (formally VSO) with credentials baked into the appSettings.

Another thing to note is that NUnit pulls out data and sends it to the parameter using ordinals. so you need to make sure the column parameter data in TFS is in the correct order as the parameters in the unit test method. I think i should be able to fix this though so it matches the parameters on the method to the Columns in TFS, I’ll have another crack in a few weeks.

The source code for the library is available here. Feel free to send a PR if you want to improve or fix anything 🙂

 

 

 

Leave a comment