It can be difficult  to write unit tests for code that accesses the file system.

It’s possible to write integration tests that read in an actual file from the file system, do some processing, and check the resultant output file (or result) for correctness. There are a number of potential problems with these types of integration tests including the potential for them to more run slowly (real IO access overheads), additional test file management/setup code, etc. (this does not mean that some integration tests wouldn’t be useful however).

The System.IO.Abstractions NuGet package can help to make file access code more testable. This package provides a layer of abstraction over the file system that is API-compatible with existing code.

Take the following code as an example:

using System.IO;
namespace ConsoleApp1
{
public class FileProcessorNotTestable
{
public void ConvertFirstLineToUpper(string inputFilePath)
{
string outputFilePath = Path.ChangeExtension(inputFilePath, ".out.txt");

using (StreamWriter outputWriter = File.CreateText(outputFilePath))
{
bool isFirstLine = true;

{

if (isFirstLine)
{
line = line.ToUpperInvariant();
isFirstLine = false;
}

outputWriter.WriteLine(line);
}
}
}
}
}


The preceding code opens a text file, and writes it to a new output file, but with the first line converted to uppercase.

This class is not easy to unit test however, it is tightly coupled to the physical file system with the calls to File.OpenText and File.CreateText.

Once the System.IO.Abstractions NuGet package is installed, the class can be refactored as follows:

using System.IO;
using System.IO.Abstractions;

namespace ConsoleApp1
{
public class FileProcessorTestable
{

public FileProcessorTestable() : this (new FileSystem()) {}

public FileProcessorTestable(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}

public void ConvertFirstLineToUpper(string inputFilePath)
{
string outputFilePath = Path.ChangeExtension(inputFilePath, ".out.txt");

using (StreamWriter outputWriter = _fileSystem.File.CreateText(outputFilePath))
{
bool isFirstLine = true;

{

if (isFirstLine)
{
line = line.ToUpperInvariant();
isFirstLine = false;
}

outputWriter.WriteLine(line);
}
}
}
}
}



The key things to notice in the preceding code is the ability to pass in an IFileSystem as a constructor parameter. The calls to File.OpenText and File.CreateText are now redirected to _fileSystem.File.OpenText and _fileSystem.File.CreateText  respectively.

If the parameterless constructor is used (e.g. in production at runtime) an instance of FileSystem will be used, however at test time, a mock IFileSystem can be supplied.

Handily, the System.IO.Abstractions.TestingHelpers NuGet package provides a pre-built mock file system that can be used in unit tests, as the following simple test demonstrates:

using System.IO.Abstractions.TestingHelpers;
using Xunit;

namespace XUnitTestProject1
{
public class FileProcessorTestableShould
{
[Fact]
public void ConvertFirstLine()
{
var mockFileSystem = new MockFileSystem();

var mockInputFile = new MockFileData("line1\nline2\nline3");

var sut = new FileProcessorTestable(mockFileSystem);
sut.ConvertFirstLineToUpper(@"C:\temp\in.txt");

MockFileData mockOutputFile = mockFileSystem.GetFile(@"C:\temp\in.out.txt");

string[] outputLines = mockOutputFile.TextContents.SplitLines();

Assert.Equal("LINE1", outputLines[0]);
Assert.Equal("line2", outputLines[1]);
Assert.Equal("line3", outputLines[2]);
}
}
}


To see this in action or to learn more about file access, check out my Working with Files and Streams in C# Pluralsight course.

In previous posts we looked at testing for thrown exceptions in xUnit.net and NUnit. In this post we’re going to see how to test in MSTest V2.

As with the previous posts, the class being tested is as follows:

public class TemperatureSensor
{
bool _isInitialized;

public void Initialize()
{
// Initialize hardware interface
_isInitialized = true;
}

{
if (!_isInitialized)
{
throw new InvalidOperationException("Cannot read temperature before initializing.");
}

return 42; // Simulate for demo code purposes
}
}


And the first test to check the normal execution:

[TestMethod]
{
var sut = new TemperatureSensor();

sut.Initialize();

Assert.AreEqual(42, temperature);
}


Next, a test can be written to check that the expected exception is thrown:

[TestMethod]
{
var sut = new TemperatureSensor();

}


The preceding code using the Assert.ThrowsException method, this method takes the type of the expected exception as the generic type parameter (in this case InvalidOperationException). As the method parameter an action/function can be specified – this is the code that is supposed to cause the exception to be thrown.

The thrown exception can also be captured if you need to test the exception property values:

[TestMethod]
{
var sut = new TemperatureSensor();

var ex = Assert.ThrowsException<InvalidOperationException>(() => sut.ReadCurrentTemperature());

Assert.AreEqual("Cannot read temperature before initializing.", ex.Message);
}


To learn more about using exceptions to handle errors in C#, check out my Error Handling in C# with Exceptions Pluralsight course or to learn more about MS Test V2 check out my Automated Testing with MSTest V2 Pluralsight course.

In a previous post, testing for thrown exceptions using xUnit.net was demonstrated. In this post we’ll see how to do the same with NUnit.

Once again the class being tested is as follows:

public class TemperatureSensor
{
bool _isInitialized;

public void Initialize()
{
// Initialize hardware interface
_isInitialized = true;
}

{
if (!_isInitialized)
{
throw new InvalidOperationException("Cannot read temperature before initializing.");
}

return 42; // Simulate for demo code purposes
}
}


The first test can be to test the happy path:

[Test]
{
var sut = new TemperatureSensor();

sut.Initialize();

Assert.AreEqual(42, temperature);
}


Next, a test can be written to check that the expected exception is thrown:

[Test]
{
var sut = new TemperatureSensor();

}


Notice in the preceding code that any InvalidOperationException thrown will pass the test. To ensure that the thrown exception is correct, it can be captured and further asserts performed against it:

[Test]
{
var sut = new TemperatureSensor();

var ex = Assert.Throws<InvalidOperationException>(() => sut.ReadCurrentTemperature());

Assert.AreEqual("Cannot read temperature before initializing.", ex.Message);
// or:
Assert.That(ex.Message, Is.EqualTo("Cannot read temperature before initializing."));
}


There’s also other ways to assert against expected exceptions such as the following:

Assert.Throws(Is.TypeOf<InvalidOperationException>()



There’s some personal preference involved when choosing a style, for example the preceding code could be considered more verbose by some and may muddle the distinction between the Act and Assert phases of a test.

To learn more about using exceptions to handle errors in C#, check out my Error Handling in C# with Exceptions Pluralsight course.

In business/personal coaching there is an idea of a “wheel of life”. The idea is that you evaluate different dimensions of your life and plot them on a wheel. Ideally the wheel should be balanced (and as large) as possible. If the wheel is crooked, you know what areas need to be improved to create a better wheel that turns more easily. When the wheel turns more easily, you get better results.

In a similar vein, the Software Development Wheel allows you to evaluate your overall software development  experience.

The tool below can be used by individual developers, the software development team as a whole, or team leaders/managers.

To generate your own wheel fill out the form below.

Each dimension is rated from 1 (“very little or none”) to 5 (“awesome, doing everything possible”) and descriptions for the dimensions are included below.

## Code Reviewing

More than one person looking at code before it is released. For example: pair programming, code reviews mob programming, etc.

## Automated Testing

Tests than can be (and are) run periodically to check that code is working as expected. For example: unit tests, integrations tests, UI tests, performance tests, security tests, etc.

## Continuous Integration

Code from developers is regularly combined together to check that it all works together. Usually automated with a build system/server such as TeamCity, Jenkins, VSTS, etc.

## Agile Practices

Codebase and development teams evolve over time. For example: incremental development, shorter feedback loops, self-organizing/cross-functional teams, etc.

## Clean Code

Code that is easier to change. For example: readable code, good object oriented practices (SOLID), YAGNI, use of appropriate design patterns, well-named variables/functions/etc.

## Supportive Management

Management exists primarily to remove things that stop/slow down the team delivering the software. For example: shielding the team from management politics, empowering individuals and teams to make decisions, caring about people, providing training, etc.

## Best Tools

The team has the best tools that are available to do their work. For example, powerful hardware, latest versions of IDEs, multiple monitors, etc.

## Work Environment

Comfortable work environment. Includes: comfortable ergonomic chairs/desks, standing desks, peace and quiet, air conditioning, plants, cleanliness, etc.

Bogus is a lovely library from Brian Chavez to use in automated tests to automatically generate test data of different kinds.

As an example suppose the following class is involved in a unit test:

public class Review
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public int Rating { get; set; }
public DateTimeOffset Created { get; set; }

public override string ToString()
{
return $"{Id} '{Title}'"; } }  In a test, a Review instance may need properties populating with values. This could be done manually, for example to check the ToString() implementation: [Fact] public void BeRepresentedAsAString() { var sut = new Review { Id = 42, Title = "blah blah" }; Assert.Equal("42 'blah blah'", sut.ToString()); }  Notice in the preceding test, the actual values and title don’t really matter, only the fact that they’re joined as part of the ToString() call. In this example the values for Id and Title could be considered anonymous variable / values in that we don’t really care about them. The following test uses the Bogus NuGet package and uses its non-fluent facade syntax: [Fact] public void BeRepresentedAsAString_BogusFacadeSyntax() { var faker = new Faker("en"); // default en var sut = new Review { Id = faker.Random.Number(), Title = faker.Random.String() }; Assert.Equal($"{sut.Id} '{sut.Title}'", sut.ToString());
}



Bogus also has a powerful fluent syntax to define what a test object will look like. To use the fluent version, a Faker<T> instance is created where T is the test object to be configured and created, for example:

[Fact]
public void BeRepresentedAsAString_BogusFluentSyntax()
{
var reviewFaker = new Faker<Review>()
.RuleFor(x => x.Id, f => f.Random.Number(1, 10))
.RuleFor(x => x.Title, f => f.Lorem.Sentence());

var sut = reviewFaker.Generate();

Assert.Equal(\$"{sut.Id} '{sut.Title}'", sut.ToString());
}


The first argument to the RuleFor() methods allows the property of the Review object to be selected and the second argument specifies how the property value should be generated. There is a huge range of test data types supported. In the preceding code the Random API is used as well as the Lorem API.

Some examples of the types of auto generated data include:

• Addresses: ZipCode, City, Country, Latitude, etc.
• Commerce: Department name, ProductName, ProductAdjective, Price, etc.
• Company: CompanyName, CatchPhrase, Bs, etc.
• Date: Past, Soon, Between, etc.
• Finance: Account number, TransactionType, Currency, CreditCardNumber, etc.
• Image URL: Random image, Animals image, Nature image, etc.
• Internet: Email, DomainName, Ipv6, Password, etc.
• Lorem: single word, Words, Sentence, Paragraphs, etc.
• Name: FirstName, LastName, etc.
• Rant: Random user review, etc.
• System: FileName, MimeType, FileExt, etc.

Some of the random generated values are quite entertaining, for example Rant.Review() may produce "My co-worker Fate has one of these. He says it looks tall."; Company.Bs() may produce "transition cross-media users", and Company.CatchPhrase() may produce "Face to face object-oriented focus group".

Bogus configuration is quite powerful and allows fairly complex setup as the following code demonstrates:

[Fact]
public void CalculateAverageRatingWhenMultipleReviews()
{
int rating = 0;

var reviewFaker = new Faker<Review>()
.RuleFor(x => x.Id, f => f.Random.Number(1, 10))
.RuleFor(x => x.Rating, f => rating++);

var productFaker = new Faker<Product>()
.RuleFor(x => x.PricePerUnit, f => f.Finance.Amount())
.RuleFor(x => x.Description, f => f.WaffleText(3))
.FinishWith((f, x) =>
{
});

var sut = productFaker.Generate();

Assert.Equal(1, sut.AverageRating); // (0 + 1 + 2) / 3
}


The WaffleText() API is provided by one of the extensions to Bogus (WaffleGenerator.Bogus) that produces inane looking waffle text such as the following:

The Quality Of Hypothetical Aesthetic

"The parallel personal hardware cannot explain all the problems in maximizing the efficacy of any fundamental dichotomies of the logical psychic principle. Generally the requirements of unequivocal reciprocal individuality is strictly significant. On the other hand the characteristic organizational change reinforces the weaknesses in the evolution of metaphysical terminology over a given time limit. The objective of the explicit heuristic discordance is to delineate the truly global on-going flexibility or the preliminary qualification limit. A priority should be established based on a combination of functional baseline and inevitability of amelioration The Quality Of Hypothetical Aesthetic"

- Michael Stringer in The Journal of the Proactive Directive Dichotomy (20174U)

structure plan.

To make the main points more explicit, it is fair to say that;
* the value of the optical continuous reconstruction is reciprocated by what should be termed the sanctioned major issue.
* The core drivers poses problems and challenges for both the heuristic non-referent spirituality and any discrete or Philosophical configuration mode.
* an anticipation of the effects of any interpersonal fragmentation reinforces the weaknesses in the explicit deterministic service. This may be due to a lack of a doctrine of the interpersonal quality..
* any significant enhancements in the strategic plan probably expresses the strategic personal theme. This trend may dissipate due to the personal milieu.

firm assumptions about ideal major monologism evinces the universe of attitude.

The Flexible Implicit Aspiration.

Within current constraints on manpower resources, any consideration of the lessons learnt can fully utilize what should be termed the two-phase multi-media program.

For example, the assertion of the importance of the integration of doctrine of the prime remediation with strategic initiatives cannot be shown to be relevant. This is in contrast to the strategic fit.


When writing tests it is sometimes useful to check that the correct exceptions are thrown at the expected time.

When using xUnit.net there are a number of ways to accomplish this.

As an example consider the following simple class:

public class TemperatureSensor
{
bool _isInitialized;

public void Initialize()
{
// Initialize hardware interface
_isInitialized = true;
}

{
if (!_isInitialized)
{
throw new InvalidOperationException("Cannot read temperature before initializing.");
}

return 42; // Simulate for demo code purposes
}
}



The first test we could write against the preceding class is to check the “happy path”:

[Fact]
{
var sut = new TemperatureSensor();

sut.Initialize();

Assert.Equal(42, temperature);
}


Next a test could be written to check that if the temperature is read before initializing the sensor, an exception of type InvalidOperationException is thrown. To do this the xUnit.net Assert.Throws method can be used. When using this method the generic type parameter indicates the type of expected exception and the method parameter takes an action that should cause this exception to be thrown, for example:

[Fact]
{
var sut = new TemperatureSensor();

}


In the preceding test, if an InvalidOperationException is not thrown when the ReadCurrentTemperature method is called the test will fail.

The thrown exception can also be captured in a variable to make further asserts against the exception property values, for example:

[Fact]
{
var sut = new TemperatureSensor();

var ex = Assert.Throws<InvalidOperationException>(() => sut.ReadCurrentTemperature());

Assert.Equal("Cannot read temperature before initializing.", ex.Message);
}


The Assert.Throws method expects the exact type of exception and not derived exceptions. In the case where you want to also allow derived exceptions, the Assert.ThrowsAny method can be used.

Similar exception testing features also exist in MSTest and NUnit frameworks.

To learn more about using exceptions to handle errors in C#, check out my Error Handling in C# with Exceptions Pluralsight course.

In a previous post I wrote about Customising the Appearance of Debug Information in Visual Studio with the DebuggerDisplay Attribute. In addition to controlling the high level  debugger appearance of an object we can also exert a lot more control over how the object appears in the debugger by using the DebuggerTypeProxy attribute.

For example, suppose we have the following (somewhat arbitrary) class:

class DataTransfer
{
public string Name { get; set; }
public string ValueInHex { get; set; }
}


By default, in the debugger it would look like the following:

To customize the display of the object members, the DebuggerTypeProxy attribute can be applied.

The first step is to create a class to act as a display proxy. This class takes the original object as part of the constructor and then exposes the custom view via public properties.

For example, suppose that we wanted a decimal display of the hex number that originally is stored in a string property in the original DataTransfer object:

class DataTransferDebugView
{

public DataTransferDebugView(DataTransfer data)
{
_data = data;
}

public string NameUpper => _data.Name.ToUpperInvariant();
public string ValueDecimal
{
get
{
bool isValidHex = int.TryParse(_data.ValueInHex, System.Globalization.NumberStyles.HexNumber, null, out var value);

if (isValidHex)
{
return value.ToString();
}

return "INVALID HEX STRING";
}
}
}


Once this view object is defined, it can be selected by decorating the DataTransfer class with the DebuggerTypeProxy attribute as follows:

[DebuggerTypeProxy(typeof(DataTransferDebugView))]
class DataTransfer
{
public string Name { get; set; }
public string ValueInHex { get; set; }
}


Now in the debugger, the following can be seen:

Also notice in the preceding image, that the original object view is available by expanding the Raw View section.

To learn more about C# attributes and even how to create your own custom ones, check out my C# Attributes: Power and Flexibility for Your Code course at Pluralsight.

In the (relatively) distant past, MSTest was often used by organizations because it was provided by Microsoft “in the box” with Visual Studio/.NET. Because of this, some organizations trusted MSTest over open source testing frameworks such as NUnit. This was at a time when the .NET open source ecosystem was not as advanced as it is today and before Microsoft began open sourcing some of their own products.

Nowadays MSTest is cross-platform and open source and is known as MSTest V2, and as the documentation states: “is a fully supported, open source and cross-platform implementation of the MSTest test framework with which to write tests targeting .NET Framework, .NET Core and ASP.NET Core on Windows, Linux, and Mac.”.

MSTest V2 provides typical assert functionality such as asserting on the values of: strings, numbers, collections, thrown exceptions, etc. Also like other testing frameworks, MSTest V2 allows the customization of the test execution lifecycle such as the running of additional setup code before each test executes. The framework also allows the creation of data driven tests (a single test method executing  multiple times with different input test data) and the ability to extend the framework with custom asserts and custom test attributes.

You can find out more about MSTest V2 at the GitHub repository, the documentation, or check out my Pluralsight course: Automated Testing with MSTest V2.

One problem when dealing with developer “secrets” in development is accidentally checking them into source control. These secrets could be connection strings to dev resources, user IDs, product keys, etc.

To help prevent this from accidentally happening, the secrets can be stored outside of the project tree/source control repository. This means that when the code is checked in, there will be no secrets in the repository.

Each developer will have their secrets stored outside of the project code. When the app is run, these secrets can be retrieved at runtime from outside the project structure.

One way to accomplish this in ASP.NET Core  projects is to make use of the Microsoft.Extensions.SecretManager.Tools NuGet package to allow use of the command line tool. (also if you are targeting .NET Core 1.x , install the Microsoft.Extensions.Configuration.UserSecrets NuGet package).

## Setting Up User Secrets

After creating a new ASP.NET Core project, add a tools reference to the NuGet package to the project, this will add the following item in the project file:

<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />


Build the project and then right click the project and you will see a new item called “Manage User Secrets” as the following screenshot shows:

Clicking menu item will open a secrets.json file and also add an element named UserSecretsId to the project file. The content of this element is a GUID, the GUID is arbitrary but should be unique for each and every project.

<UserSecretsId>c83d8f04-8dba-4be4-8635-b5364f54e444</UserSecretsId>


User secrets will be stored in the secrets.json file which will be in %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json on Windows or ~/.microsoft/usersecrets/<user_secrets_id>/secrets.json on Linux and macOS. Notice these paths contain the user_secrets_id that matches the GUID in the project file. In this way each project has a separate set of user secrets.

The secrets.json file contains key value pairs.

## Managing User Secrets

User secrets can be added by editing the json file or by using the command line (from the project directory).

To list user secrets type: dotnet user-secrets list At the moment his will return “No secrets configured for this application.”

To set (add) a secret: dotnet user-secrets set "Id" "42"

The secrets.json file now contains the following:

{
"Id": "42"
}


Other dotnet user-secrets  commands include:

• clear - Deletes all the application secrets
• list - Lists all the application secrets
• remove - Removes the specified user secret
• set - Sets the user secret to the specified value

## Accessing User Secrets in Code

To retrieve users secrets, in the startup class, access the item by key, for example:

public void ConfigureServices(IServiceCollection services)
{

var secretId = Configuration["Id"]; // returns 42
}


One thing to bear in mind is that secrets are not encrypted in the secrets.json file, as the documentation states: “The Secret Manager tool doesn't encrypt the stored secrets and shouldn't be treated as a trusted store. It's for development purposes only. The keys and values are stored in a JSON configuration file in the user profile directory.” & “You can store and protect Azure test and production secrets with the Azure Key Vault configuration provider.”

There’s a lot more information in the documentation and if you plan to use this tool you should read through it.

Just because serverless allows us to quickly deploy value, it doesn’t mean that testing is now obsolete. (click to Tweet)

If we’re using Azure Functions as our serverless platform we can write our code (for example C#) and test it before deploying to Azure. In this case we’re talking about precompiled Azure Functions as opposed to earlier incarnations of Azure Functions that used .csx script files.

Working with precompiled functions means the code can be developed and tested on a local development machine. The code we write is familiar C# with some additional attributes to integrate the code with the Azure Functions runtime.

Because the code is just regular C#, we can use familiar testing tools such as MSTest, xUnit.net, or NUnit. Using these familiar testing frameworks it’s possible to write tests that operate at different levels of granularity.

One way to categorize these tests are into:

• Unit tests to check core business logic/value
• Integration tests to check function run methods are operating correctly
• End-to-end workflow tests that check multiple functions working together

To enable effective automated testing it may be necessary to write functions in such a way as to make them testable, for example by allowing function run method dependencies to be automatically injected at runtime, whereas at test time mock versions can be supplied for example using a framework such as AzureFunctions.Autofac.

There are other tools that allow us to more easily test functions locally such as the local functions runtime and the Azure storage emulator.

To learn more about using these tools and techniques to test Azure Functions, check out my Pluralsight course Testing Precompiled Azure Functions: Deep Dive.