SpecFlow is a tool that allows the writing of business-readable tests that can then be automated in code. If you’re new to SpecFlow check out my Pluralsight course to get up to speed before looking at these more advanced topics.
Individual step definitions that represent the natural language scenarios are separate methods that get executed. Often we need to pass data between these steps. The simplest approaches to get started with are to use simple fields in the class containing the steps or to use the Scenario Context. An alternative to these approaches is to use Context Injection.
For example, imagine the following scenario:
Scenario: Add New Contact Name
Given I have entered Sarah
When I choose add
Then the contact list should show the new contact
With my new Pluralsight course you’ll learn how to narrow the gap between the business/customer and the development team by creating business-readable, automated tests.
The course covers how to install SpecFlow in Visual Studio, the Gherkin business-readable domain specific language (DSL), and how to create code-automation from this natural language.
You can find the course on my Pluralsight author page.
xUnit.net allows the creation of data-driven tests. These kind of tests get their test data from outside the test method code via parameters added to the test method signature.
Say we had to test a Calculator class and check it’s add method returned the correct results for six different test cases. Without data-driven tests we’d either have to write six separates tests (with almost identical code) or some loop inside our test method containing an assert.
Regular xUnit.net test methods are identified by applying the [Fact] attribute. Data-driven tests instead use the [Theory] attribute.
To get data-driven features and the [Theory] attribute, install the xUnit.net Extensions NuGet package in addition to the standard xUnit.net package.
Creating Inline Data-Driven Tests
The [InlineData] attribute allows us to specify test data that gets passed to the parameters of test method.
So for our Calculator add test we’d start by defining the test method:
To help people get started with xUnit.net and as an accompaniment to my Pluralsight xUnit.net training course I thought I’d create a cheat sheet showing common assert methods and attributes. Hopefully will be of use :)
My latest Pluralsight course on the xUnit.net testing framework has just been released.
Learn the latest in unit testing technology for C#, VB.NET (and other .NET languages) created by the original inventor of NUnit.
xUnit.net is a free, extensible, open source framework designed for programmers that aligns more closely with the .NET platform.
You can check it out now on Pluralsight.com.
My newest Pluralsight course has just been published.
We shouldn't live in fear of our code
Long-term customer satisfaction, agility, and developer happiness are crucial. A quality suite of automated tests helps achieve this. This practical course covers how and what to test at the unit, integration, and functional UI levels; and how to bring them all together with TeamCity continuous integration build server.
The course helps to keep your software soft with the right automated tests at the right level.
If you’ve never used Pluralsight before you can sign up for the free trial and get started.
Just knocked up this quick and dirty video explaining how to set up MoqaLate in a Windows Store app solution.
You can find out more about MoqaLate on the MoqaLate project home page.
With Windows Store apps there are challenges getting traditional mocking frameworks such as Rhino and Moq working due to limited reflection support in the platform (presumably for security reasons).
I wrote a mocking solution when Windows Phone 7 first came out, it can also be used for Windows Store apps. I’ve updated the NuGet descriptions etc. to reflect this.
How To Do TDD with Mocking in Windows Store Apps
Create a new (C#/XAML) Windows Store app project in Visual Studio called “MyAwesomeApp”.
Create your test project “MyAwesomeApp.Tests” and reference your main app.
In the main app project, install the MoqaLate NuGet package. When the package is installed you will have a new folder in the main app solution called “MoqaLateCommandLine”:
(Inside this folder is a readmexxx.txt file with some additional info)
The below is an excerpt from the latest chapter “An Introduction to Unit Testing With xUnit.net” from my book Keeping Software Soft.
xUnit.net provides a number of ways for checking (asserting) results are as expected.
The following explanatory tests shown the different types of assertions that xUnit.net supports:
public class XUnitAssertExamples
public void SimpleAssertsThatOneValueEqualsAnother()
Assert.Equal(1, 2); // fail
Assert.Equal("hello", "hello"); // pass
Assert.NotEqual(1, 2); // pass
Assert.NotEqual("hello", "hello"); // fail
public void BooleanAsserts()
Assert.True(true); // pass
Assert.True(false); // fail
Assert.False(false); // pass
Assert.False(true); // fail
// Don't do this
Assert.True(1 == 1); // pass
public void Ranges()
const int value = 22;
Assert.InRange(value, 21, 100); // pass
Assert.InRange(value, 22, 100); // pass
Assert.NotInRange(value, 999, 99999); // pass
Assert.InRange(value, 23, 100); // fail
public void Nulls()
Assert.Null(null); // pass
Assert.NotNull("hello"); // pass
Assert.NotNull(null); // fail
public void ReferenceEquality()
var objectA = new Object();
var objectB = new Object();
Assert.Same(objectA, objectB); // fail
Assert.NotSame(objectA, objectB); // pass
public void AnIEnumberableContainsASpecificItem()
var days = new List<string>
Assert.Contains("Monday", days); // pass
Assert.Contains("Friday", days); // fail
Assert.DoesNotContain("Friday", days); // pass
public void IEnumerableEmptiness()
var aCollection = new List<string>();
Assert.Empty(aCollection); // pass
aCollection.Add("now no longer empty");
Assert.NotEmpty(aCollection); // pass
Assert.Empty(aCollection); // fail
public void IsASpecificType()
Assert.IsType<string>("hello"); // pass
Assert.IsNotType<int>("hello"); // pass
Assert.IsType<int>("hello"); // fail
public void IsAssignableFrom()
const string stringVariable = "42";
Assert.IsAssignableFrom<string>(stringVariable); // pass
Assert.IsAssignableFrom<int>(stringVariable); // fail
To learn more about xUnit.net check out my Pluralsight course: The xUnit.net testing framework. Also check out my xUnit.net cheat sheet.
Until xUnit officially supports* Windows store apps you can get xUnit working with your WinRT app by doing the following:
In Visual Studio 2012, go to Tools menu, Extensions and Updates; search for and install “xUnit.net runner for Visual Studio 2012”.
In Visual Studio 2012, go to Tools menu, Extensions and Updates; search for and install “xUnit Test Library Template”.
You may have to restart Visual Studio…