What is NXUnit?
NXUnit is a NUnit -style unit testing framework about XML for .NET Framework. It is an extension to NUnit. It brings you the ability to do unit testing easily in XML applications. It helps you to concentrate on business logic of your XML application and improve your Test Driven Development(TDD) technics. You can directly compare one XML string or stream with another, er assert that they are equal, just like doing the same thing to two integers using xUnit. But without NXUnit, you must pay attention to whitespaces in XML strings, empty elements or attributes, unimportant order of elements or attributes, unneccessary comments and so on. It's similar with XmlUnit in some aspects.
Features
The current version is NXUnit 1.0rc1, July 2005. The following is the 8 features of this version, which you can find in the facade class XMLAssert:
* Assert that two XML inputs are equal.
* Compare two XML inputs and find all differences between them.
* Assert that declarations of two XML inputs are equal.
* Assert that document types of two XML inputs are equal.
* Assert the validity of an XML input.
* Assert that the evaluation of an XPath expression on an XML input will return the expected value.
* Assert that an XPath expression exists for an XML input.
* Assert that an XML input is included by another.
And you can change the properties of an instance of XMLAssert before an assertion or comparition, in order to:
* Ignore the case of the elements' and attributes' names
* Ignore XML comments
* Ignore XML declarations and document types of both inputs
* Ignore empty elements and attributes
* Ignore orders of elements and attributes
* Ignore unimportant whitespaces
Sample
1
using System;
2
using System.IO;
3
using System.Xml;
4
using NUnit.Framework;
5
using NXUnit.Framework;
6
7
8
[TestFixture]
9
public class Sample
10
{
11
private XMLAssert xa;
12
13
[SetUp]
14
public void Init()
15
{
16
xa = XMLAssert.CreateInstance();
17
}
18
19
[Test]
20
public void TestMethod()
21
{
22
// Init the xml input
23
string s1 = "
";
24
string s2 = "
";
25
26
// Init the options for your purpose
27
xa.IsOrderSensitive = false;
28
29
// Assert two XML inputs are equal
30
xa.AreEqual(s1, s2, "Assertion Failed!");
31
32
// Compare two XML inputs and find all differences between them
33
CompareResult r = xa.Compare(s1, s2);
34
foreach (Diff d in r)
35
{
36
Console.WriteLine(d);
37
}
38
CompareResult another = xa.Compare(s1, s2);
39
r.Add(another);
40
for (int i = 0; i < r.Count; i++)
41
{
42
Console.WriteLine(r[i]);
43
}
44
if (r.AreEqual)
45
{
46
// They are equal
47
}
48
49
// Assert two XML declaration of the two XML inputs are equal
50
xa.AreDeclareEqual(s1, s2, "Declarations are not equal");
51
52
// Assert two document types of the two XML inputs are equal
53
xa.AreDocTypeEqual(s1, s2, "DocTypes are not equal");
54
55
// Assert the validity of an XML input
56
XMLAssert.IsValid("
");
57
XMLAssert.IsValidFile(@"C:\
");
58
59
// Assert the evaluation of an XPath expression on an XML input will
60
// return the expected value
61
xa.AreXpathEqual("<a/>", "/r/a[2]", s1,
62
"The xpath expression doesn't return <a/>");
63
64
// Assert an XPath expression is exist for an XML input
65
XMLAssert.XpathExist("//@b='c'", s1,
66
"The xml document doesn't have the xpath expression");
67
68
// Assert an XML input is included by another one
69
xa.IsIncluded(s1, s2, "The {0} is not included in {1}", s1, s2);
70
71
// The Counter
72
Assert.AreEqual(6, xa.Counter);
73
74
// XMLInput can use in all the samples above
75
xa.AreEqual(XMLInput.CreateFromString(s1),
76
XMLInput.CreateFromString(s2), "Assertion Failed!");
77
}
78
}
posted @
2005-07-15 13:22 Brian Sun 阅读(1937) |
评论 (2) |
编辑 收藏
首先,要向大家致以我深深的歉意,因为我已经有两个多月没来关心我的这个与朋友们交流的平台了。原因有很多,最主要的原因就是太忙了。起先是忙写毕业论文,这件事很让我头疼了一把,因为我要一边上班一边完成毕业论文,于是每当我打开Blog想添一篇新文章的时候我就会立即想到还要论文要写,于是立即打开 word毫不含糊。论文忙完了之后,我着手写一篇很长的文章,记录了我这三个月来对“测试驱动开发(TDD)”的理解和体验,这篇文章对我来说非常重要,也是我一段时间工作的总结,于是我想等我把整篇文章写完之后再奉献给大家比较好。这篇文章刚有了构思和提纲之后我又收到北京总部发来的消息,要求我们每日加班到9:00,周六也工作,这耗费了我之前用来写Blog的几乎全部时间,再之后就是转出消息领导要来南京视察,尤其是视察我们小组的工作,于是我又要加班加点的赶进度,因为我们小组的几个人进度都不能另自己满意(更不用说领导了

)。再之后就是领导没来,但是要求我们尽快去北京参加评审和一些开不完的会议,于是我们又Z50去Z49回来,中间转正花了一周开会花了一周。然后我就回家洗了个澡,睡了个安稳觉,然后就到今天了。
还有一件事情也是最近一段时间值得骄傲的,要提一下,就是我花了点时间把自己写的一个小工具包装成了一个开源软件,在SourceForge上登了出来,是一个单元测试工具(当然也可以叫TDD工具啦,哈哈,

),对NUnit的一个扩展,使其可以方便的比较和断言两个XML,希望能对遇到同样问题的朋友们带来一些帮助。所谓“花了点时间”是指我把每个周日都耗在了提着笔记本在Starbucks要一杯Frappuccino然后写十几个小时的代码和文档上了。
但是,再多的借口也不能弥补我在春夏换季时不写Blog的罪过。所以我决心从今天开始洗心革面,重新做人,每周都写Blog,大家再给我一次机会吧!

。。。。。。
致歉的泡泡
posted @
2005-07-15 12:47 Brian Sun 阅读(1044) |
评论 (7) |
编辑 收藏