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:
The equality performance of struct equality comparisons can be improved by overriding .Equals(). This is especially true if the structs we are comparing contain reference type fields.
By default, the equality of structs is automatically determined by doing a byte-by-byte comparison of the two struct objects in memory – only when the structs don’t contain any reference types. When the structs contain reference type fields then reflection is used to compare the fields of the two struct objects which results in slower performance.
This chart show the relative performance of the default equality of a struct that contains only value types against a struct that also contains a reference type:
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.
We can create generic classes such as:
public void Write(T thing)
Here we’re defining a generic class, then using the type T as a method parameter in the Write method. To use this class we’d write something like:
var w = new ThingWriter<int>();
We don’t however have to be working in a generic class to make use of generic methods.
If we’ve got chars with numeric digits in them, we can convert them to numeric (double) values using the char.GetNumericValue method.
The following code:
double d = char.GetNumericValue('5');
Outputs the value: 5
So why does GetNumericValue return a double when a char can only be a single character and hold a single “number” (‘0’ to ‘9’)? Because char holds Unicode characters.
So the following code:
double d = char.GetNumericValue('⅔');
Outputs the value: 0.666666666666667
Hence the need for GetNumericValue to return a double.
For more C# tips check out my Pluralsight courses: C# Tips and Traps and C# Tips and Traps 2.
My previous article “Non Short Circuiting C# Conditional Operators” garnered some comments which suggested to me that I should expand on it. There can sometimes be some confusion over the & and && operator in C# so hopefully this article goes some way to helping clarify the differences between them.
The && Operator
Works with: bools
The && operator performs a logical AND on Boolean values.
For example, the following code:
bool b1 = true;
bool b2 = false;
bool isBothTrue = b1 && b2;
If we get our two bool values from the result of a couple of methods instead, and the first method returns false, the && operator won’t execute the second method. The following code:
Most of the time when writing C# programs we use the short-circuiting conditional operators. These operators do not continue evaluation once the outcome has been determined.
Take this code:
bool b = false && CheckName(name);
b = false & CheckName(name);
Here we’re performing a logical AND with the value false and the result of the CheckName method. Because we have a false, the logical AND can never be true.
The first statement using the short-circuiting AND operator (“&&”) will not execute the CheckName method because as soon as it’s determined the outcome (the first value is false and we are AND-ing) the remaining terms are not even evaluated.
My latest Pluralsight course has just been released. It’s the follow-up course to the popular first C# Tips and Traps course.
Whether you're still learning C# or you already have some experience, it's sometimes hard to know what you don't know. This is the follow-up course to C# Tips and Traps and is designed to further short-circuit your C# learning and provides a whole host of useful information about the sometimes under-used or unknown features of both the C# language and the .Net framework. It's suitable for those who are brand new to C# as well as experienced developers looking to "round off" their C# skills and "fill in the gaps".
Check out the course table of contents for more details.