In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-05-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the usefulness of xUnit.Net for unit testing tools in .NET development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
XUnit.Net introduction
The creator of xUnit.net is a new framework summed up by Jim Newkirk and Brad Wilson from the experience of using NUnit and other unit testing frameworks, which has the following features compared to NUnit,xUnit.net:
Generate an object instance for each test method
Canceled [SetUp] and [TearDown]
Canceled [ExpectedException]
Functions similar to Aspect
Reduced the number of custom attributes (Attribute)
Adopt generics
Anonymous delegation
Extensible assertion
Extensible test method
Extensible test class
The official website of xUnit.net is http://xunit.codeplex.com. The following is the operating interface of xUnit.net:
Note that there are four exe files running in GUI mode in the downloaded xUnit.net package, which are:
Xunit.gui.clr4.exe: used to run xUnit.net under x64 and .net 4.0.
Xunit.gui.clr4.x86.exe: used to run xUnit.net under x86 and .net 4.0.
Xunit.gui.exe: used to run xUnit.net on x64 and below. Net 4.0.
Xunit.gui.x86.exe: used to run xUnit.net on x86 and below. Net 4.0.
Download and install xUnit.Net
Common Attribute tags for xUnit.Net
If you have previously used NUnit or VS's native MSTest testing tool, the following comparison table will make it easy for you to get started with xUnit.net:
NUnit 2.2
MSTest
XUnit.net
Remarks
[Test]
[TestMethod]
[Fact]
Mark as test method
[TestFixture]
[TestClass]
N/a
Classes that contain test methods, which do not need to be marked in xUnit.net, will find test methods for all public in the assembly
[ExpectedException]
[ExpectedException]
Assert.Throws/ Record.Exception
Expect to throw an exception
[SetUp]
[TestInitialize]
Constructor (that is, constructor)
The method used to initialize before each test method is executed
[TearDown]
[TestCleanup]
IDisposable.Dispose
The method used to end after each test method is executed
[TestFixtureSetUp]
[ClassInitialize]
IUseFixture
Methods used to initialize before all test methods are executed
[TestFixtureTearDown]
[ClassCleanup]
IUseFixture
The method used to end after all test methods have been executed
[Ignore]
[Ignore]
[Fact (Skip= "reason")]
Temporarily ignore flagged methods
N/a
[Timeout]
[Fact (Timeout=n)]
Used to specify the maximum execution time (in milliseconds) of the method being tested, which will be marked as a test failure if the specified time is exceeded
[Property]
[TestProperty]
[Trait]
Set arbitrary metadata on a test
N/a
[DataSource]
[Theory], [XxxData]
XUnit.Net 's assertion (Assertions)
The table below is also a comparison of NUnit, MSTest, and xUnit.Net assertions.
NUnit 2.2
MSTest
XUnit.net
Remarks
AreEqual
AreEqual
Equal
Equal comparison
AreNotEqual
AreNotEqual
NotEqual
Unequal comparison
AreNotSame
AreNotSame
NotSame
Different comparison
AreSame
AreSame
Same
Same comparison
Contains
Contains (on CollectionAssert)
Contains
DoAssert
N/a
N/a
N/a
DoesNotContain (on CollectionAssert)
DoesNotContain
N/a
N/a
DoesNotThrow
Fail
Fail
N/a
Can be replaced by Assert.True (false, "message")
Greater
N/a
N/a
Can be replaced by Assert.True (x > y)
Ignore
Inconclusive
N/a
N/a
N/a
InRange
IsAssignableFrom
N/a
IsAssignableFrom
IsEmpty
N/a
Empty
IsFalse
IsFalse
False
IsInstanceOfType
IsInstanceOfType
IsType
IsNaN
N/a
N/a
Can be replaced by Assert.True (double.IsNaN (x))
IsNotAssignableFrom
N/a
N/a
Can be replaced by Assert.False (obj is Type)
IsNotEmpty
N/a
NotEmpty
IsNotInstanceOfType
IsNotInstanceOfType
IsNotType
IsNotNull
IsNotNull
NotNull
IsNull
IsNull
Null
IsTrue
IsTrue
True
Less
N/a
N/a
Available Assert.True (x
< y)替代 n/a n/a NotInRange 确保数据在某个范围内 n/a n/a Throws 确保会抛出异常 xUnit.Net的项目文件结构 因为在可视化方面xUnit.Net不如NUnit,所以这里有必要介绍一下它的项目文件.xunit的组成元素。.xunit实际上也是一个xml文件,它的根节点是,有子节点,下可以有多个节点。节点包含以下属性: filename:这是必须属性,用于指定包含在项目中的绝对或者相对路径的文件。 config-filename:这个是非必须属性,用于指定测试时所使用的config文件,默认是none,表示不适用任何配置文件。 shadow-copy:运行测试时是否对dll进行shadow-copy,默认是true,这个咱还不清楚true/false对程序的影响。 下面就是一个例子,在例子中执行指明了测试中使用的config文件: xUnit.Net的使用 xUnit.Net的常见用法很简单,对于属性NUnit的筒子们来说,轻松掌握xUnit.net的常见用法不在话下,下面就是一个简单例子(实现添加了config文件并做了相应配置,详情见本系列之一): using System; using Xunit; using System.Configuration; namespace XunitDemo { public class XunitDemo:IDisposable { public XunitDemo() { //在这里可以做测试开始前的初始化工作 System.Console.WriteLine("Init"); } [Fact] public void TestAdd() { Assert.Equal(5, 2 + 3); } [Fact(Timeout=900)]//指定超时为900ms public void TestTimeout() { System.Threading.Thread.Sleep(1000); Assert.InRange(new Random().NextDouble()*10,5,10); } [Fact] public void Test0_51CTOBlog() { //不区分大小写等值判断 Assert.Equal(true,string.Equals(ConfigurationManager.AppSettings["51ctoBlog"], "http://zhoufoxcn.blog.51cto.com",StringComparison.InvariantCultureIgnoreCase); } [Fact] public void Test0_CSDNBlog() { Assert.Equal(ConfigurationManager.AppSettings["CSDNBlog"], "http://blog.csdn.net/zhoufoxcn"); } [Fact] public void Test0_SinaWeiBo() { Assert.Equal(ConfigurationManager.AppSettings["SinaWeiBo"], "http://weibo.com/zhoufoxcn"); } public void Dispose() { //在这里可以做测试结束后的收尾工作 System.Console.WriteLine("Dispose"); } } } 程序运行效果如下:Summary
As an improved version of NUnit, xUnit.Net does overcome many shortcomings of NUnit (for the shortcomings and shortcomings of NUnit, see the previous article "Unit testing tools in .NET Development (1)-NUnit"). Compared with NUnit's Assert API, xUnit.Net 's Assert is more concise but sufficient to meet the needs of unit testing. In contrast, NUnit's Assert API is slightly bloated (this may be related to its support from .net 1.1 and being backward compatible), but in terms of GUI ease of use, xUnit.Net is not as easy as NUnit,NUnit 's GUI provides a lot of configuration interface, so that configuration can be done through interface settings, but the same work in xUnit.Net needs to be done by configuring nodes in the project file (such as specifying config files).
In addition, both NUnit and xUnit.Net support VS to some extent. For example, you can use xunit.installer.exe in the same directory of xUnit.Net to configure the support for VS. The following figure shows the interface when running xunit.installer.exe:
Thank you for reading! This is the end of the article on "what is the use of xUnit.Net of unit testing tools in .NET development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.