EDennis.NetCoreTestingUtilities 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EDennis.NetCoreTestingUtilities --version 1.0.0                
NuGet\Install-Package EDennis.NetCoreTestingUtilities -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EDennis.NetCoreTestingUtilities" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EDennis.NetCoreTestingUtilities --version 1.0.0                
#r "nuget: EDennis.NetCoreTestingUtilities, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EDennis.NetCoreTestingUtilities as a Cake Addin
#addin nuget:?package=EDennis.NetCoreTestingUtilities&version=1.0.0

// Install EDennis.NetCoreTestingUtilities as a Cake Tool
#tool nuget:?package=EDennis.NetCoreTestingUtilities&version=1.0.0                

Overview

This package utilizes the Newtonsoft JSON library to provide methods that make testing of objects and object collections a little easier. The package is intended to be used in Test projects only, as it uses reflection extensively and is not performance-optimized.

Object Extension Methods

The following extension methods for objects are provided:

bool IsSame(T obj)

This method returns true if the provided object variable and the current object variable (this) refer to the same object in memory. The method compares hashes and property values. Example:

[Test]
public void TestSameObjectInMemory() {
    Person p1 = new Person("Bob", "Jones");
    Person p2 = p1; //assign p2 to point to p1 (same object in memory)
    Assert.True(p1.IsSame(p2)); 
}

bool IsEqual(T obj)

This method returns true if the provided object variable and the current object variable (this) have the same property values. NOTE: this is a deep comparison. Example:

[Test]
public void TestEqualObjects() {
    Person p1 = new Person("Bob", "Jones");
    Person p2 = new Person("Bob", "Jones");
    Assert.False(p1.IsSame(p2));
    Assert.True(p1.IsEqual(p2));
}

bool IsEqual(T obj, string[] pathsToIgnore)

This method returns true if the provided object variable and the current object variable (this) have the same property values, ignoring all properties at the provided JSON paths. Example:

[Test]
public void TestEqualObjectsIgnoringSelectedProperties() {
    Person p1 = new Person(1, "Bob", "Jones");
    Person p2 = new Person(2, "Bob", "Jones");
    Assert.False(p1.IsEqual(p2));
    Assert.True(p1.IsEqual(p2, new string[] { "ID" })); //ignore ID
}

string ToJsonString()

This method serializes an object to a JSON string. Example:

[Test]
public void TestToJsonString() {
    string expectedJson =
        @"{
         ""ID"": 0,
         ""FirstName"": ""David"",
         ""LastName"": ""Parks""
        }";
    expectedJson = JToken.Parse(expectedJson).ToString(Formatting.Indented);
    Person p1 = new Person("David", "Parks");
    string actualJson = p1.ToJsonString();
    Assert.AreEqual(expectedJson, actualJson);
}

T FromJsonString<T>(string json)

This method creates a new object of type T by deserializing the provided JSON string. When called from an object constructor, this method provides a convenient way to load an object with properties. Example:

[Test]
public void TestFromJsonString() {
    string json =
        @"{
         ""FirstName"": ""David"",
         ""LastName"": ""Parks""
        }";
    var p1 = new Person().FromJsonString(json);
    Person p2 = new Person("David", "Parks");
    Assert.True(p1.IsEqual(p2));
}

T FromJsonPath<T>(JToken jtoken, string jsonPath)

This method creates a new object of type T by deserializing a JSON object or array at the provided path in the provided JToken object. When called from an object constructor, this method provides a convenient way to load an object with properties. Example:

[Test]
public void TestFromJsonPath1() {
    string json =
       @"{
         ""FirstName"": ""David"",
         ""LastName"": ""Parks"",
         ""Contact"": {
             ""FirstName"": ""Jill"",
             ""LastName"": ""Parks""
            }
        }";
    var jtoken = JToken.Parse(json);
    var p1 = new Person().FromJsonPath(jtoken, "Contact");
    Person p2 = new Person("Jill", "Parks");
    Assert.True(p1.IsEqual(p2));
}

T FromJsonPath<T>(string filePath, string objectPath)

This method creates a new object of type T by deserializing a JSON object or array in the provided JSON file at the provided path. When called from an object constructor, this method provides a convenient way to load an object with properties. NOTE: you can use "" or "/" instead of "." in the objectPath. Example:

[Test]
public void TestFromJsonPath2() {
    var p1 = new Person().FromJsonPath(@"DavidParks.json", "Contact");
    Person p2 = new Person("Jill", "Parks");
    Assert.True(p1.IsEqual(p2));
}

T FromJsonPath<T>(string jsonFileObjectPath)

This method is equivalent to the above method, but it combines the filePath and objectPath into a single expression. Note that you can use a backslash instead of period in the objectPath. Example:

[Test]
public void TestFromJsonPath3() {
    var p1 = new Person().FromJsonPath(@"DavidParks.json\Contact");
    Person p2 = new Person("Jill", "Parks");
    Assert.True(p1.IsEqual(p2));
}

JsonAssert Class

A JsonAssert Class is provided with an static, overloaded ExecuteTest method that reads in a test case from a JSON file in a specific test folder/subfolder and executes the test case. This method makes numerous assumptions about the format of the JSON file and its location. When these assumptions are met, the C# code needed to invoke the method from a test class is minimal. Here are the assumptions:

  • The method under test is an instance method, not a static method.
  • The JSON file consists of a single JSON object whose property names (except for one) exactly match the parameter names for the method under test.
  • The expected return value for the test case is represented by a "returns" property in the JSON file.
  • All data within the JSON file reflect properly serialized values, which are parseable by Json.NET.

static void ExecuteTest<T>(T obj, string testCase)

This method accepts an instantiated object (obj) and a file name excluding folder and extension(testCase) as parameters. It throws a JsonAssertException if the test case fails. This method makes some additional assumptions:

  • The JSON test case must be placed in a subfolder whose name is the same as the method to be tested.
  • The method subfolder must be placed in a top-level folder whose name is the same as the name of the class to be tested.
  • The JSON file has a .json extension.

Example:

{
  "persons": [
    {
      "FirstName": "David",
      "LastName": "Parks",
      "DateOfBirth": "1987-06-29"
    },
    {
      "FirstName": "Johnny",
      "LastName": "Sandoval",
      "DateOfBirth": "2014-12-06"
    }
  ],
  "returns":     {
      "FirstName": "Johnny",
      "LastName": "Sandoval",
      "DateOfBirth": "2014-12-06"
    }
}
/// <summary>
/// Assumes the PersonManager class has an instance method
/// called Youngest and that for each of the provided testCase 
/// values in the range (1-2), there is a properly formatted
/// JSON file residing in the right folder and subfolder
/// (e.g., "PersonManager\Youngest\01.json")
/// </summary>
/// <param name="testCase">The name of the test case</param>
[Test]
public void Youngest([Range(1, 2)] int testCase) {
    JsonAssert.ExecuteTest(new PersonManager(), testCase.ToString("D2"));
}

static void ExecuteTest<T>(T obj, string filePath, string methodName, string testCase)

This method is the same as the previous method, but it allows the caller to explicitly set the file path and the name of the method under test.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EDennis.NetCoreTestingUtilities:

Package Downloads
EDennis.AspNetCore.Base

Alpha version that still requires (a) updated code comments, (b) updated wiki documentation, and (c) completed sample solution

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.8.6 1,221 2/4/2020
3.8.5 725 2/3/2020
3.8.4 741 2/3/2020
3.8.3 951 2/3/2020
3.8.2 819 1/28/2020
3.8.1 657 1/22/2020
3.8.0 703 1/22/2020
3.7.9 858 1/11/2020
3.7.8 808 1/11/2020
3.7.7 735 1/9/2020
3.7.6 807 1/9/2020
3.7.5 792 1/9/2020
3.7.4 757 1/9/2020
3.7.3 716 1/9/2020
3.7.2 794 1/4/2020
3.7.1 732 1/2/2020
3.7.0 665 1/2/2020
3.6.7 781 12/19/2019
3.6.6 728 12/19/2019
3.6.5 767 12/18/2019
3.6.4 730 12/12/2019
3.6.3 709 12/12/2019
3.6.2 673 12/12/2019
3.6.1 719 12/10/2019
3.6.0 732 10/22/2019
3.5.0 718 10/8/2019
3.4.8 808 9/12/2019
3.4.7 789 9/10/2019
3.4.6 830 8/16/2019
3.4.5 819 6/21/2019
3.4.3 776 5/7/2019
3.4.2 785 5/7/2019
3.4.1 785 5/3/2019
3.4.0 6,257 3/25/2019
3.3.1 808 3/5/2019
3.3.0 812 3/5/2019
3.2.2 5,118 2/20/2019
3.2.1 820 2/8/2019
3.2.0 903 12/15/2018
3.1.0 1,579 11/19/2018
3.0.0 1,567 9/25/2018
2.9.9 1,514 9/24/2018
2.9.8 1,565 9/24/2018
2.9.7 1,773 9/21/2018
2.9.6 1,652 9/21/2018
2.9.5 1,816 9/21/2018
2.9.4 1,736 9/20/2018
2.9.3 1,749 9/18/2018
2.9.2 1,759 9/18/2018
2.9.1 1,649 9/17/2018
2.9.0 1,584 9/11/2018
2.8.4 1,024 8/16/2018
2.8.3 1,071 8/10/2018
2.8.2 1,091 8/9/2018
2.8.1 1,138 8/9/2018
2.8.0 1,126 8/9/2018
2.7.2 1,101 8/7/2018
2.7.1 1,110 8/7/2018
2.7.0 1,026 8/6/2018
2.6.0 1,080 8/2/2018
2.5.4 1,078 7/23/2018
2.5.3 1,060 7/23/2018
2.5.2 1,184 6/21/2018
2.5.1 1,098 6/15/2018
2.5.0 1,174 6/6/2018
2.4.1 1,103 6/4/2018
2.4.0 1,171 5/25/2018
2.3.9 1,229 5/2/2018
2.3.8 1,198 4/17/2018
2.3.7 1,342 4/6/2018
2.3.6 1,286 3/23/2018
2.3.5 1,176 3/9/2018
2.3.4 1,211 3/9/2018
2.3.3 1,077 3/8/2018
2.3.2 1,089 3/8/2018
2.3.1-alpha 1,142 3/6/2018
2.2.4 1,216 3/3/2018
2.2.3.1 1,227 3/3/2018
2.2.3 1,192 3/3/2018
2.2.2 1,160 2/21/2018
2.2.1 1,168 2/21/2018
2.2.0 1,174 2/10/2018
2.1.1 1,112 2/8/2018
2.1.0.1 1,269 12/21/2017
2.1.0 1,311 12/21/2017
2.0.0 1,179 12/21/2017
1.0.0.1 1,246 12/3/2017
1.0.0 1,595 11/30/2017

Initial Release