nunit

单元测试之道-使用NUnit(c#)

空扰寡人 提交于 2020-02-09 08:30:40
这几天,在网上找了些关于单元测试的使用说明(NUnit).由于初学,就拿自己的测试用例来说说吧. 1. TDD的简介 首先什么是TDD呢?Kent Beck在他的<<测试驱动开发 >>(Addison-Wesley Professional,2003)一书中,使用下面2个原则来定义TDD: · 除非你有一个失败的自动测试,永远不要写一单行代码. · 阻止重复 我想第一个原则是显而易见的.在没有失败的自动测试下就不要写代码.因为测试是嵌入在代码必须满足的需求中.如果没有需求,就没有必要实现任何东西.所以这个原则阻止我们去实现那些没有测试和在解决方案中不需要的功能. 第二个原则说明了在一个程序中,不应该包含重复的代码.如果代码重复,我想这就是不好的软件设计的象征.随着时间的流逝,它会对程序造成不一致的问题,并且使代码变非常混乱 ,因为我们时常不会记得重复代码的位置.如果发现代码重复,我想我们应该立即删除代码重复.其实这就涉及到重构了.在这里我就不多讲了. 一般来说,测试分为2种类型,一是程序员自己的测试,另外一种是客户的测试.关于客户测试,我推荐一个FIT的框架,非常不错。在这里,我们讲的TDD就是程序员测试.那么什么是程序员测试呢?我认为就是我们常说的单元测试.既然是单元测试,在.NET里势必会用到某些工具,目前最著名恐怕就是我即将介绍的NUnit了, 2.NUnit的介绍

NUnit2.0详细使用方法

爱⌒轻易说出口 提交于 2020-02-09 08:30:06
前一段时间,有人问我在.NET里如何进行TDD开发.这个问题促使我想对NUnit做一个详细的介绍.因为我们大家都知道NUnit是在.NET进行TDD的利器. 如果你已经知道很多关于NUnit的应用,请指出我的不对之处和提出一些建议,使本文更加完善.如果你对NUnit还不是很了解的话,我建议你还是阅读一下. 本文分为以下部分: 1. TDD的简介 首先什么是TDD呢?Kent Beck在他的<<测试驱动开发 >>(Addison-Wesley Professional,2003)一书中,使用下面2个原则来定义TDD: · 除非你有一个失败的自动测试,永远不要写一单行代码. · 阻止重复 我想第一个原则是显而易见的.在没有失败的自动测试下就不要写代码.因为测试是嵌入在代码必须满足的需求中.如果没有需求,就没有必要实现任何东西.所以这个原则阻止我们去实现那些没有测试和在解决方案中不需要的功能. 第二个原则说明了在一个程序中,不应该包含重复的代码.如果代码重复,我想这就是不好的软件设计的象征.随着时间的流逝,它会对程序造成不一致的问题,并且使代码变非常混乱 ,因为我们时常不会记得重复代码的位置.如果发现代码重复,我想我们应该立即删除代码重复.其实这就涉及到重构了.在这里我就不多讲了. 一般来说,测试分为2种类型,一是程序员自己的测试,另外一种是客户的测试.关于客户测试

NUnit单元测试属性介绍

主宰稳场 提交于 2020-02-09 08:29:26
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using NUnit.Framework; 6 using System.Diagnostics; 7 using System.Threading; 8 9 namespace Test 10 { 11 /**/ /// <summary> 12 /// TestFixture属性标记表示该类是一个带有测试方法的类,注意: 13 /// 该类的访问修饰符必须是public 14 /// </summary> 15 [TestFixture] 16 public class TestClass 17 { 18 19 private string dateTime = DateTime.Now.ToString(); 20 private string dateTime2 = DateTime.Now.ToString(); 21 /**/ /// <summary> 22 /// 构造函数 23 /// </summary> 24 public TestClass() { } 25 26 /**/ /// <summary> 27 /// SetUp属性用来修饰方法

Skipping Feature - SpecFlow C#

耗尽温柔 提交于 2020-02-06 10:18:50
问题 I'm looking to intercept a test using the [BeforeFeature] SpecFlow Hook and ignore the entire feature file. private static string FeatureName = FeatureContext.Current.FeatureInfo.Title; [BeforeFeature] public static void BeforeFeature() { Console.WriteLine("Before feature"); if (TestFilter.ShouldBeIgnored(FeatureName)) { // Ignore Feature if it matches TestFilter Requirements } } 回答1: If you are using Specflow + Nunit, you can call Assert.Ignore("ignore message here"); This will cause the

How to log NUnit Test Error or fail message?

扶醉桌前 提交于 2020-02-05 04:56:24
问题 I have a set of NUnit tests running and I would like to log the results of the test (along with some environment information) to a DB in the Teardown method after each test completes. Is there any way to get that information from the NUnit TestContext apart from writing my own NUnit add-in? I know that the fail or error messages get logged to whatever output file I specify with the console runner, but I would really like to do it programmatically. 回答1: You have access to the TestContext

How to log NUnit Test Error or fail message?

拥有回忆 提交于 2020-02-05 04:56:03
问题 I have a set of NUnit tests running and I would like to log the results of the test (along with some environment information) to a DB in the Teardown method after each test completes. Is there any way to get that information from the NUnit TestContext apart from writing my own NUnit add-in? I know that the fail or error messages get logged to whatever output file I specify with the console runner, but I would really like to do it programmatically. 回答1: You have access to the TestContext

Does .NET Core work with NUNIT console runner?

痞子三分冷 提交于 2020-02-04 02:01:27
问题 I have a very simple test! [Test] public void TestMethod1() { Assert.IsTrue(false); } When I try to run this with nunit3-console.exe I get the following error. NUnit.Engine.NUnitEngineException : The NUnit 3 driver encountered an error while executing reflected code. ----> System.InvalidCastException : Unable to cast transparent proxy to type 'System.Web.UI.ICallbackEventHandler'. --NUnitEngineException I am running .NET Core 2.1, with NUNIT Console Runner 3.9, NUnit test adapter 3.1. I did

MSBuild to NUnit transform (VS2010)

萝らか妹 提交于 2020-01-25 11:23:48
问题 I tried to use the xslt mentioned in this SO question to turn MSTest trx into NUnit Xml (to get round a bug with MStest output in Bamboo) but seem to have no joy. Is the trx format different for VS2010 ? (or is it just me?) 回答1: The namespace has changed. Just replace xmlns:t="http://microsoft.com/schemas/VisualStudio/TeamTest/2006" with xmlns:t="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" and it should work. 来源: https://stackoverflow.com/questions/5537461/msbuild-to-nunit

Mocked method returns null when using anonymous types

风流意气都作罢 提交于 2020-01-25 02:57:25
问题 I have this code: using NSubstitute; using NUnit.Framework; using System; using System.Linq.Expressions; namespace MyTests { public interface ICompanyBL { T GetCompany<T>(Expression<Func<Company, T>> selector); } public partial class Company { public int RegionID { get; set; } } public class Tests { [Test] public void Test() { var companyBL = Substitute.For<ICompanyBL>(); //Doesn't work companyBL.GetCompany(c => new { c.RegionID }).Returns(new { RegionID = 4, }); //Results in null: var

How to run NUnit test in STA thread?

匆匆过客 提交于 2020-01-24 19:27:35
问题 We are in the process of porting a WPF app to .NET Core 3, preview 5. Some NUnit tests need to run in STA threads. How can this be done? None of the attributes like [STAThread], [RequiresSTA], ... work. This also doesn't work: [assembly: RequiresThread(ApartmentState.STA)] The Apparent namespace does not seem to be exposed in .NET Core 3. Has anyone done this? 回答1: ApartmentAttribute was first enabled for .NET Standard 2.0 in NUnit 3.12. First update your version of the NUnit framework, then