NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考官方文档。初次写博客,望大家指点。

导航:

单元测试之NUnit一

单元测试之NUnit二

单元测试之NUnit三

除了Assert断言外,NUnit还提供了其它直接断言的方法,比如string、directory、file等。另外,如有需要还可自行扩展。

String Assert

实现了string 类型的断言,包含以下方法:

 public class StringAssertTest
{
[Test]
public void Contains_Test()
{
string expected = "a";//期望值。被包含
string actual = "acb";//真是值。包含
StringAssert.Contains(expected, actual);
expected = "d";
StringAssert.DoesNotContain(expected, actual);
} [Test]
public void StartsWith_Test()
{
string actual = "dcc";
string expected = "d";
StringAssert.StartsWith(expected, actual);
expected = "c";
StringAssert.DoesNotStartWith(expected, actual);
} [Test]
public void EndsWith_Test()
{
string actual = "dcc";
string expected = "c";
StringAssert.EndsWith(expected, actual);
expected = "d";
StringAssert.DoesNotEndWith(expected, actual);
} [Test]
public void EqualIgnoringCase_Test()
{
string actual = "adc";
string expected = "adc";
StringAssert.AreEqualIgnoringCase(expected, actual);
expected = "a";
StringAssert.AreNotEqualIgnoringCase(expected, actual);
} [Test]
public void Match_Test()
{
string actual = "adc";
string expected = "adc";
StringAssert.IsMatch(expected, actual);
expected = "g";
StringAssert.DoesNotMatch(expected, actual);
}
}

Collection Assert

CollectionAssert 提供为实现了IEnumerable的集合比较提供一系列方法:

[Test]
public void Equal_Test()
{
List<int> expected = new List<int> { 1, 2, 3 };
List<int> actual = new List<int> { 2, 3, 1 };
CollectionAssert.AreNotEqual(expected, actual);//集合元素位置不同,不相等
actual = new List<int> { 1, 2, 3 };
CollectionAssert.AreEqual(expected, actual);
} [Test]
public void ItemsOfType_Test()
{
List<object> cols = new List<object> { "1", "2", "b" };
CollectionAssert.AllItemsAreInstancesOfType(cols, typeof(string));
}

更多方法如下:

CollectionAssert.AllItemsAreInstancesOfType(IEnumerable collection, Type expectedType);
CollectionAssert.AllItemsAreInstancesOfType(
IEnumerable collection, Type expectedType, string message, params object[] args); CollectionAssert.AllItemsAreNotNull(IEnumerable collection);
CollectionAssert.AllItemsAreNotNull(
IEnumerable collection, string message, params object[] args); CollectionAssert.AllItemsAreUnique(IEnumerable collection);
CollectionAssert.AllItemsAreUnique(
IEnumerable collection, string message, params object[] args); CollectionAssert.AreEqual(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreEqual(
IEnumerable expected, IEnumerable actual, string message, params object[] args); CollectionAssert.AreEquivalent(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreEquivalent(
IEnumerable expected, IEnumerable actual, string message, params object[] args); CollectionAssert.AreNotEqual(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreNotEqual(
IEnumerable expected, IEnumerable actual, string message, params object[] args); CollectionAssert.AreNotEquivalent(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreNotEquivalent(
IEnumerable expected, IEnumerable actual, string message, params object[] args); CollectionAssert.Contains(IEnumerable expected, object actual);
CollectionAssert.Contains(
IEnumerable expected, object actual, string message, params object[] args); CollectionAssert.DoesNotContain(IEnumerable expected, object actual);
CollectionAssert.DoesNotContain(
IEnumerable expected, object actual, string message, params object[] args); CollectionAssert.IsSubsetOf(IEnumerable subset, IEnumerable superset);
CollectionAssert.IsSubsetOf(
IEnumerable subset, IEnumerable superset, string message, params object[] args); CollectionAssert.IsNotSubsetOf(IEnumerable subset, IEnumerable superset);
CollectionAssert.IsNotSubsetOf(
IEnumerable subset, IEnumerable superset, string message, params object[] args); CollectionAssert.IsEmpty(IEnumerable collection);
CollectionAssert.IsEmpty(
IEnumerable collection, string message, params object[] args); CollectionAssert.IsNotEmpty(IEnumerable collection);
CollectionAssert.IsNotEmpty(
IEnumerable collection, string message, params object[] args); CollectionAssert.IsOrdered(IEnumerable collection);
CollectionAssert.IsOrdered(
IEnumerable collection, string message, params object[] args); CollectionAssert.IsOrdered(IEnumerable collection, IComparer comparer);
CollectionAssert.IsOrdered(IEnumerable collection,
IComparer comparer, string message, params object[] args);

Directory Assert

文件夹断言:

        [Test]
public void Directory_Test()
{
string basePath = AppDomain.CurrentDomain.BaseDirectory;
DirectoryAssert.Exists(basePath + "/Directory"); // 判断文件夹是否存在
}
 DirectoryAssert.AreEqual(DirectoryInfo expected, DirectoryInfo actual);
DirectoryAssert.AreEqual(DirectoryInfo expected, DirectoryInfo actual,
string message, params object[] args); DirectoryAssert.AreNotEqual(DirectoryInfo expected, DirectoryInfo actual);
DirectoryAssert.AreNotEqual(DirectoryInfo expected, DirectoryInfo actual,
string message, params object[] args); DirectoryAssert.Exists(DirectoryInfo actual);
DirectoryAssert.Exists(DirectoryInfo actual,
string message, params object[] args); DirectoryAssert.Exists(string actual);
DirectoryAssert.Exists(string actual,
string message, params object[] args); DirectoryAssert.DoesNotExist(DirectoryInfo actual);
DirectoryAssert.DoesNotExist(DirectoryInfo actual,
string message, params object[] args); DirectoryAssert.DoesNotExist(string actual);
DirectoryAssert.DoesNotExist(string actual,
string message, params object[] args);

File Assert

文件断言:

        [Test]
public void File_Test()
{
// 判断文件是否存在
FileAssert.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Directory/file.txt");
}
FileAssert.AreEqual(Stream expected, Stream actual); //判断两个文件流是否相同
FileAssert.AreEqual(
Stream expected, Stream actual, string message, params object[] args); FileAssert.AreEqual(FileInfo expected, FileInfo actual); // 判断两个文件是否同一路径的同一文件
FileAssert.AreEqual(
FileInfo expected, FileInfo actual, string message, params object[] args); FileAssert.AreEqual(string expected, string actual);
FileAssert.AreEqual(
string expected, string actual, string message, params object[] args); FileAssert.AreNotEqual(Stream expected, Stream actual);
FileAssert.AreNotEqual(
Stream expected, Stream actual, string message, params object[] args); FileAssert.AreNotEqual(FileInfo expected, FileInfo actual);
FileAssert.AreNotEqual(
FileInfo expected, FileInfo actual, string message, params object[] args); FileAssert.AreNotEqual(string expected, string actual);
FileAssert.AreNotEqual(
string expected, string actual, string message, params object[] args); FileAssert.Exists(FileInfo actual); //判断文件是否存在
FileAssert.Exists(
FileInfo actual, string message, params object[] args); FileAssert.Exists(string actual);
FileAssert.Exists(
string actual, string message, params object[] args); FileAssert.DoesNotExist(FileInfo actual);
FileAssert.DoesNotExist(
FileInfo actual, string message, params object[] args); FileAssert.DoesNotExist(string actual);
FileAssert.DoesNotExist(
string actual, string message, params object[] args);

扩展

NUnit本身提供非常全面的功能,一般来说完全满足我们的项目需求。即使不能满足需求,NUnit也是可扩展的,包括:属性、约束条件、断言等的扩展。文档导航

04-25 20:22