EDennis.NetCoreTestingUtilities
1.0.0
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
<PackageReference Include="EDennis.NetCoreTestingUtilities" Version="1.0.0" />
paket add EDennis.NetCoreTestingUtilities --version 1.0.0
#r "nuget: EDennis.NetCoreTestingUtilities, 1.0.0"
// 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 | Versions 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. |
-
.NETCoreApp 2.0
- JsonDiffPatch (>= 2.0.45)
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,215 | 2/4/2020 |
3.8.5 | 721 | 2/3/2020 |
3.8.4 | 737 | 2/3/2020 |
3.8.3 | 947 | 2/3/2020 |
3.8.2 | 815 | 1/28/2020 |
3.8.1 | 653 | 1/22/2020 |
3.8.0 | 699 | 1/22/2020 |
3.7.9 | 854 | 1/11/2020 |
3.7.8 | 804 | 1/11/2020 |
3.7.7 | 731 | 1/9/2020 |
3.7.6 | 803 | 1/9/2020 |
3.7.5 | 788 | 1/9/2020 |
3.7.4 | 753 | 1/9/2020 |
3.7.3 | 712 | 1/9/2020 |
3.7.2 | 790 | 1/4/2020 |
3.7.1 | 728 | 1/2/2020 |
3.7.0 | 661 | 1/2/2020 |
3.6.7 | 777 | 12/19/2019 |
3.6.6 | 724 | 12/19/2019 |
3.6.5 | 763 | 12/18/2019 |
3.6.4 | 726 | 12/12/2019 |
3.6.3 | 705 | 12/12/2019 |
3.6.2 | 669 | 12/12/2019 |
3.6.1 | 715 | 12/10/2019 |
3.6.0 | 728 | 10/22/2019 |
3.5.0 | 714 | 10/8/2019 |
3.4.8 | 804 | 9/12/2019 |
3.4.7 | 785 | 9/10/2019 |
3.4.6 | 826 | 8/16/2019 |
3.4.5 | 815 | 6/21/2019 |
3.4.3 | 772 | 5/7/2019 |
3.4.2 | 781 | 5/7/2019 |
3.4.1 | 781 | 5/3/2019 |
3.4.0 | 6,253 | 3/25/2019 |
3.3.1 | 804 | 3/5/2019 |
3.3.0 | 808 | 3/5/2019 |
3.2.2 | 5,114 | 2/20/2019 |
3.2.1 | 815 | 2/8/2019 |
3.2.0 | 897 | 12/15/2018 |
3.1.0 | 1,572 | 11/19/2018 |
3.0.0 | 1,558 | 9/25/2018 |
2.9.9 | 1,505 | 9/24/2018 |
2.9.8 | 1,556 | 9/24/2018 |
2.9.7 | 1,764 | 9/21/2018 |
2.9.6 | 1,643 | 9/21/2018 |
2.9.5 | 1,807 | 9/21/2018 |
2.9.4 | 1,727 | 9/20/2018 |
2.9.3 | 1,740 | 9/18/2018 |
2.9.2 | 1,750 | 9/18/2018 |
2.9.1 | 1,640 | 9/17/2018 |
2.9.0 | 1,575 | 9/11/2018 |
2.8.4 | 1,015 | 8/16/2018 |
2.8.3 | 1,062 | 8/10/2018 |
2.8.2 | 1,082 | 8/9/2018 |
2.8.1 | 1,129 | 8/9/2018 |
2.8.0 | 1,117 | 8/9/2018 |
2.7.2 | 1,091 | 8/7/2018 |
2.7.1 | 1,102 | 8/7/2018 |
2.7.0 | 1,018 | 8/6/2018 |
2.6.0 | 1,071 | 8/2/2018 |
2.5.4 | 1,070 | 7/23/2018 |
2.5.3 | 1,052 | 7/23/2018 |
2.5.2 | 1,176 | 6/21/2018 |
2.5.1 | 1,090 | 6/15/2018 |
2.5.0 | 1,164 | 6/6/2018 |
2.4.1 | 1,095 | 6/4/2018 |
2.4.0 | 1,163 | 5/25/2018 |
2.3.9 | 1,221 | 5/2/2018 |
2.3.8 | 1,190 | 4/17/2018 |
2.3.7 | 1,334 | 4/6/2018 |
2.3.6 | 1,278 | 3/23/2018 |
2.3.5 | 1,168 | 3/9/2018 |
2.3.4 | 1,203 | 3/9/2018 |
2.3.3 | 1,069 | 3/8/2018 |
2.3.2 | 1,081 | 3/8/2018 |
2.3.1-alpha | 1,138 | 3/6/2018 |
2.2.4 | 1,208 | 3/3/2018 |
2.2.3.1 | 1,219 | 3/3/2018 |
2.2.3 | 1,184 | 3/3/2018 |
2.2.2 | 1,152 | 2/21/2018 |
2.2.1 | 1,160 | 2/21/2018 |
2.2.0 | 1,166 | 2/10/2018 |
2.1.1 | 1,104 | 2/8/2018 |
2.1.0.1 | 1,262 | 12/21/2017 |
2.1.0 | 1,304 | 12/21/2017 |
2.0.0 | 1,172 | 12/21/2017 |
1.0.0.1 | 1,239 | 12/3/2017 |
1.0.0 | 1,587 | 11/30/2017 |
Initial Release