ChainingAssertion-MSTest.Bin
2.1.1
dotnet add package ChainingAssertion-MSTest.Bin --version 2.1.1
NuGet\Install-Package ChainingAssertion-MSTest.Bin -Version 2.1.1
<PackageReference Include="ChainingAssertion-MSTest.Bin" Version="2.1.1" />
paket add ChainingAssertion-MSTest.Bin --version 2.1.1
#r "nuget: ChainingAssertion-MSTest.Bin, 2.1.1"
// Install ChainingAssertion-MSTest.Bin as a Cake Addin #addin nuget:?package=ChainingAssertion-MSTest.Bin&version=2.1.1 // Install ChainingAssertion-MSTest.Bin as a Cake Tool #tool nuget:?package=ChainingAssertion-MSTest.Bin&version=2.1.1
Chaining Assertion
Method Chaining base UnitTesting Extension Methods and Dynamic Private Accessor for MSTest, NUnit, xUnit.net. NuGet install supported.
Binary only edition packages
This edition provides .dll file as general NuGet package style.
- PM> Install-Package ChainingAssertion-MSTest.Bin (for MSTest)
- PM> Install-Package ChainingAssertion-NUnit.Bin (for NUnit)
- PM> Install-Package ChainingAssertion-xUnit.Bin (for xUnit.net)
Is
easy to write, you can use only one method.
// "Is" extend on all object and has three overloads.
// This same as Assert.AreEqual(25, Math.Pow(5, 2))
Math.Pow(5, 2).Is(25);
// lambda predicate assertion.
// This same as Assert.IsTrue("foobar".StartsWith("foo") && "foobar".EndWith("bar"))
"foobar".Is(s => s.StartsWith("foo") && s.EndsWith("bar"));
// collection assert takes variable arguments
// This same as CollectionAssert.AreEqual(Enumerable.Range(1,5), new[]{1, 2, 3, 4, 5})
Enumerable.Range(1, 5).Is(1, 2, 3, 4, 5);
CollectionAssert
if you want to use CollectionAssert Methods then use Linq to Objects and Is.
var array = new[] { 1, 3, 7, 8 };
array.Count().Is(4);
array.Contains(8).IsTrue(); // IsTrue() == Is(true)
array.All(i => i < 5).IsFalse(); // IsFalse() == Is(false)
array.Any().Is(true);
new int[] { }.Any().Is(false); // IsEmpty
array.OrderBy(x => x).Is(array); // IsOrdered
Other Assertions
// Null Assertions
Object obj = null;
obj.IsNull(); // Assert.IsNull(obj)
new Object().IsNotNull(); // Assert.IsNotNull(obj)
// Not Assertion
"foobar".IsNot("fooooooo"); // Assert.AreNotEqual
new[] { "a", "z", "x" }.IsNot("a", "x", "z"); /// CollectionAssert.AreNotEqual
// ReferenceEqual Assertion
var tuple = Tuple.Create("foo");
tuple.IsSameReferenceAs(tuple); // Assert.AreSame
tuple.IsNotSameReferenceAs(Tuple.Create("foo")); // Assert.AreNotSame
// Type Assertion
"foobar".IsInstanceOf<string>(); // Assert.IsInstanceOfType
(999).IsNotInstanceOf<double>(); // Assert.IsNotInstanceOfType
Advanced Collection Assertion
var lower = new[] { "a", "b", "c" };
var upper = new[] { "A", "B", "C" };
// Comparer CollectionAssert, use IEqualityComparer<T> or Func<T,T,bool> delegate
lower.Is(upper, StringComparer.InvariantCultureIgnoreCase);
lower.Is(upper, (x, y) => x.ToUpper() == y.ToUpper());
// or you can use Linq to Objects - SequenceEqual
lower.SequenceEqual(upper, StringComparer.InvariantCultureIgnoreCase).Is(true);
StructuralEqual
class MyClass
{
public int IntProp { get; set; }
public string StrField;
}
var mc1 = new MyClass() { IntProp = 10, StrField = "foo" };
var mc2 = new MyClass() { IntProp = 10, StrField = "foo" };
mc1.IsStructuralEqual(mc2); // deep recursive value equality compare
mc1.IntProp = 20;
mc1.IsNotStructuralEqual(mc2);
DynamicAccessor
AsDynamic convert to "dynamic" that can call private method/property/field/indexer.
// a class and private field/property/method.
public class PrivateMock
{
private string privateField = "homu";
private string PrivateProperty
{
get { return privateField + privateField; }
set { privateField = value; }
}
private string PrivateMethod(int count)
{
return string.Join("", Enumerable.Repeat(privateField, count));
}
}
// call private property.
var actual = new PrivateMock().AsDynamic().PrivateProperty;
Assert.AreEqual("homuhomu", actual);
// dynamic can't invoke extension methods.
// if you want to invoke "Is" then cast type.
(new PrivateMock().AsDynamic().PrivateMethod(3) as string).Is("homuhomuhomu");
// set value
var mock = new PrivateMock().AsDynamic();
mock.PrivateProperty = "mogumogu";
(mock.privateField as string).Is("mogumogu");
Is(lambda)'s error message
// a model
public class Person
{
public int Age { get; set; }
public string FamilyName { get; set; }
public string GivenName { get; set; }
}
[TestMethod]
public void TestMethod()
{
// this assertion is fail
var person = new Person { Age = 50, FamilyName = "Yamamoto", GivenName = "Tasuke" };
person.Is(p => p.Age <= 10 && p.FamilyName == "Yamada" && p.GivenName == "Tarou");
}
error message shows property values
Exception Test (for MSTest)
// Exception Test(alternative of ExpectedExceptionAttribute)
// AssertEx.Throws does not allow derived type
// AssertEx.Catch allows derived type
// AssertEx.ThrowsContractException catch only Code Contract's ContractException
AssertEx.Throws<ArgumentNullException>(() => "foo".StartsWith(null));
AssertEx.Catch<Exception>(() => "foo".StartsWith(null));
AssertEx.ThrowsContractException(() => /* contract method */);
// return value is occured exception
var ex = AssertEx.Throws<InvalidOperationException>(() =>
{
throw new InvalidOperationException("foobar operation");
});
ex.Message.Is(s => s.Contains("foobar")); // additional exception assertion
// must not throw any exceptions
AssertEx.DoesNotThrow(() =>
{
// code
});
Parameterized Test (for MSTest)
Parameterized Test like NUnit's TestCase. TestCase takes parameters and send to TestContext's Extension Method "Run".
[TestClass]
public class UnitTest
{
public TestContext TestContext { get; set; }
[TestMethod]
[TestCase(1, 2, 3)]
[TestCase(10, 20, 30)]
[TestCase(100, 200, 300)]
public void TestMethod2()
{
TestContext.Run((int x, int y, int z) =>
{
(x + y).Is(z);
(x + y + z).Is(i => i < 1000);
});
}
}
TestCaseSource (for MSTest)
TestCaseSource can take static field/property that types is only object[][].
[TestMethod]
[TestCaseSource("toaruSource")]
public void TestTestCaseSource()
{
TestContext.Run((int x, int y, string z) =>
{
string.Concat(x, y).Is(z);
});
}
public static object[] toaruSource = new[]
{
new object[] {1, 1, "11"},
new object[] {5, 3, "53"},
new object[] {9, 4, "94"}
};
Parameterized Test - Async version (for MSTest)
There is also "RunAsync" extension method that is async version of "Run" extesion method.
[TestClass]
public class UnitTest
{
public TestContext TestContext { get; set; }
[TestMethod]
[TestCase(1, 2, 3)]
[TestCase(10, 20, 30)]
[TestCase(100, 200, 300)]
public async Task TestTestCaseAsync()
{
await TestContext.RunAsync((int x, int y, int z) =>
{
var actual = await SumAsync(x, y);
actual.Is(z);
});
}
}
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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.0.1)
- MSTest.TestFramework (>= 1.2.0)
-
.NETStandard 2.1
- Microsoft.CSharp (>= 4.0.1)
- MSTest.TestFramework (>= 1.2.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ChainingAssertion-MSTest.Bin:
Package | Downloads |
---|---|
ChainingAssertion.Bin
[DEPRECATED] Please use the "ChainingAssertion-MSTest.Bin" package instead. Now, this package is a metadata-only package and just redirects to the "ChainingAssertion-MSTest.Bin". / Method Chaining base UnitTesting Extension Methods and Dynamic Private Accessor for MSTest. |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on ChainingAssertion-MSTest.Bin:
Repository | Stars |
---|---|
jsakamoto/ipaddressrange
.NET Class Library for range of IP address, both IPv4 and IPv6.
|
v.2.1.1
- Enhance: Support for the latest version of MSTest
- Fix: Incorrect method resolution when using dynamic accessor by "AsDynamic()"