Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
Introducing UnitTester: A Powerful Tool for Unit Test Data Logging
I am excited to introduce a new abstract class called UnitTester in the DotNetTips.Spargine.Tester assembly. This class is designed to help developers easily log and output results from unit test data to the Debug output or save it to a file. By providing a set of simple yet powerful methods for logging object properties, UnitTester is an invaluable tool for debugging and reporting during unit testing.
Key Features and Methods
The UnitTester class offers several methods to streamline logging and output tasks in unit tests. Here is a breakdown of the key methods:
- CleanupOutputDirectory(string searchPattern = “*.txt”)
Deletes all files matching the specified search pattern from the output directory. - MeasureAction(Action action, bool printResult = true)
Executes the specified action and returns the elapsed time, optionally printing the result to the debug output. There is also a MeasureActionAsync() method. - OutputDirectory
Gets the output directory where files will be saved. - PrintToDebug([NotNull] string input)
Writes the specified input string to the debug output. - PrintToDebug(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector)
Prints the properties of each object in a collection to the Debug output based on the selection function, making it easy to inspect multiple objects at once.
This method is overloaded to use a single input (not a collection). - PrintToDebug(string input)
Writes the specified input string to the debug output. - PrintToDebug(T input, Func<PropertyInfo, bool> propertySelector)
Prints the properties of a single object to the Debug output, which is useful for inspecting individual test data objects. - PropertiesToString(T input, Func<PropertyInfo, bool> propertySelector)
Converts the properties of an object into a string representation based on a selection function. This method is protected and intended for use within derived classes. - SaveAsJsonToFile<T>(T input)
Serializes the specified object to JSON and saves it to a file in the output directory. - SaveToFile(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector, DirectoryInfo directory)
Saves the properties of each object in a collection to a file in the specified directory. This method generates a file name based on the calling method name. If the method name is empty, a random key is used as the file name. The file is saved with a .txt extension in the specified directory. Each object in the collection is converted to a string representation using the property selector, and each object’s properties are written as a separate line in the file. - SaveToFile(string input, DirectoryInfo directory)
Saves the specified input string to a file in the specified directory. This method generates a file name based on the calling method name. If the method name is empty, a random key is used as the file name. The file is saved with a .txt extension in the specified directory. - SaveToFile(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector)
Saves the selected properties of each object in a collection to a file in the current directory. This is handy for persistent logs or for sharing results. - SaveToFile(string input)
Saves the specified input string to a file in the output directory. - SaveToFile(T input, Func<PropertyInfo, bool> propertySelector, DirectoryInfo directory)
Saves the properties of an object to a file in the specified directory. This method generates a file name based on the calling method name. If the method name is empty, a random key is used as the file name. The file is saved with a .txt extension in the specified directory. The object’s properties are converted to a string representation using the property selector function, formatting each property as “PropertyName: PropertyValue” and joining them with comma-space separators. - SaveToFileAsync(string input)
Asynchronously saves the specified input string to a file in the output directory. - SaveToFile(T input, Func<PropertyInfo, bool> propertySelector)
Saves the properties of a single object to a file in the current directory. This is perfect for testing single items and capturing the data for further analysis. - SaveToFileAsync(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector, DirectoryInfo directory)
Asynchronously saves the properties of each object in a collection to a file in the specified directory. This method generates a file name based on the calling method name. If the method name is empty,a random key is used as the file name. The file is saved with a .txt extension in the specified directory. Each object in the collection is converted to a string representation using the property selector, and each object’s properties are written as a separate line in the file. - SaveToFileAsync(IEnumerable<T> collection, Func<PropertyInfo, bool> propertySelector)
Asynchronously saves the properties of each object in a collection to a file in the current directory.
Example Usage
Here is an example demonstrating how to use the UnitTester class in a unit test scenario:
[TestClass]
public class AssemblyHelperUnitTester : UnitTester
{
[TestMethod]
public void FindNetSDKFiles_SpecificVersion_ReturnsFiles()
{
// Act
var result = AssemblyHelper.GetNetSdkDllFiles("8.0.15");
// Output properties to Debug for verification
PrintToDebug<FileInfo>(result, prop => prop.Name == "Name");
// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.Count > 0, "Expected to find .NET SDK files for version 8.0.15, but none were found.");
}
}
Explanation of the Example
In the example above:
- Act: We call the method AssemblyHelper.GetNetSdkDllFiles(“8.0.15”) to retrieve a list of .NET SDK files for version 8.0.15.
- PrintToDebug: We use the
PrintToDebugmethod to log theNameproperty of eachFileInfoobject in the result collection to the Debug output. This helps in verifying that the correct files are being returned. - Assert: We then assert that the result is not null and contains at least one file, ensuring that SDK files for the specified version are found.
Summary
The UnitTester class is an essential tool for .NET developers looking to streamline their unit test workflows. By providing easy-to-use methods for logging object properties to the Debug output or saving them to files, UnitTester enhances the debugging process and makes it easier to report and verify test data. All methods are optimized for performance, making it a valuable asset in any developer’s toolkit.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.


One thought on “Boost Your .NET Projects: Mastering Unit Testing with Spargine’s UnitTester Class”