Tag: nunit

NUnit中的数据驱动测试?

在MSTest中,您可以执行以下操作: [TestMethod] [DataSource(“Microsoft.VisualStudio.TestTools.DataSource.CSV”, “testdata.csv”, “testdata#csv”, DataAccessMethod.Sequential)] public void TestSomething() { double column1 = Convert.ToDouble(TestContext.DataRow[“column1”]); … Assert.AreEqual(…); } NUnit 2.5中的等效代码是什么?

nunit如何成功等待async void方法完成?

在C#中使用async/await时,一般规则是避免async void因为它几乎是火,忘了,如果没有从方法发送返回值,则应该使用Task 。 说得通。 但奇怪的是,在本周早些时候,我正在为我编写的一些async方法编写一些unit testing,并注意到NUnit建议将async测试标记为void或返回Task 。 然后我试了一下,果然,它奏效了。 这看起来很奇怪,因为nunit框架如何能够运行该方法并等待所有异步操作完成? 如果它返回Task,它可以等待任务,然后做它需要做的事情,但是如果它返回void它怎么能把它拉下来呢? 所以我破解了源代码并找到了它。 我可以在一个小样本中重现它,但我根本无法理解它们正在做什么。 我想我对SynchronizationContext以及它是如何工作的了解不够。 这是代码: class Program { static void Main(string[] args) { RunVoidAsyncAndWait(); Console.WriteLine(“Press any key to continue. . .”); Console.ReadKey(true); } private static void RunVoidAsyncAndWait() { var previousContext = SynchronizationContext.Current; var currentContext = new AsyncSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(currentContext); try { var myClass = new MyClass(); var […]

如何运行NUnit测试?

我想有一个独立的项目,它将测试通过USB连接的远程系统的一些function。 所以我想在我的应用程序中使用NUnit的所有function。 我目前写道: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using NUnit.Framework; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.ReadLine(); } } [TestFixture] public class MyTest { [Test] public void MyTest() { int i = 3; Assert.AreEqual(3, i); } } } 如何运行我的测试套件以及如何获得测试报告?

NSubstitute vs PRISM EventAggregator:断言调用方法会触发具有正确有效负载的事件

考虑以下方法,通过PRISM EventAggregator更新人员并发布事件,以指示此人已更新。 我想unit testing消息是否与正确的有效负载一起发送。 在这种情况下,这意味着正确的personId。 public void UpdatePerson(int personId) { // Do whatever it takes to update the person // … // Publish a message indicating that the person has been updated _eventAggregator .GetEvent() .Publish(new PersonUpdatedEventArgs { Info = new PersonInfo { Id = personId, UpdatedAt = DateTime.Now }; }); } 我知道我可以创建事件聚合器的替代品: var _eventAggregator = […]

使用NUnit尽早初始化log4Net

我想知道在NUnit项目中初始化log4Net的最佳方法是什么。 当然我想尽快调用init代码(即XmlConfigurator.Configure() )来获取尽可能多的早期日志输出。 但由于我的项目是通过NUnit运行的,因此我对其入口点几乎无法控制。 根据NUnit文档,它应首先调用一些构造函数,然后在标有[TestFixtureSetup]的类中使用[TestFixtureSetup] [SetUp]属性标记的方法。 所以,首先,我创建了一个静态助手类,我可以多次调用它而不会出现问题。 public static class LoggingFacility { private static bool _loggerIsUp = false; public static void InitLogger() { if (_loggerIsUp == false) XmlConfigurator.ConfigureAndWatch(f); _loggerIsUp = true; } } 然后,我使所有[TestFixtureSetup]inheritance了一个除了调用LoggingFacility.initLogger()之外几乎没有的东西。 但是,这仍然会使所有早期运行的构造函数按照我只能随机假设的顺序运行。 而且,在我甚至无法执行某些代码之前,它可能会进行一些静态初始化。 事实上,正如我在日志中看到的那样,执行的前4秒左右完全没有记录。 这是否意味着我必须在每个构造函数中调用我的InitLogger()并禁止使用任何静态初始化程序? 那是艰苦的工作! 有人知道这个魔术吗?

NUnit Mocking不适用于Singleton方法

忍受我,我是NUnit的新手。 我来自Rails之地,所以其中一些对我来说是新的。 我有一行代码如下: var code = WebSiteConfiguration.Instance.getCodeByCodeNameAndType(“CATALOG_Brands_MinQty”, item.Catalog); 我试图模仿它,就像这样(假设code已经初始化): var _websiteConfigurationMock = new DynamicMock(typeof(WebSiteConfiguration)); _websiteConfigurationMock.ExpectAndReturn(“getCodeByCodeNameAndType”, code); 当我调试测试时, getCodeByCodeNameAndType返回null ,而不是预期的code 。 我究竟做错了什么? NUnit版本:2.2.8

NUnit中不支持的测试框架错误

我正在使用Visual Studio 2013进行NUnit测试。我们使用NUnitTestAdapter将NUnit的测试运行与Visual Studio集成。 Visual Studio 2013 NUnit版本=“3.0.1”NUnitTestAdapter版本=“2.0.0”.Net Framework 4.5.2 所有包都是最新的,并从Nuget安装。 没有构建错误。 我们在测试结果窗口中收到错误: Attempt to load assembly with unsupported test framework in D:\JuniorAchievement\Git\jaums\JA.UMS.Tests\bin\Debug\JA.UMS.Tests.dll 使用Visual Studio Test Explorer运行或调试测试时。 测试能够在Visual Studio 2013 ultimate上使用相同代码的一台计算机上运行。 我们所有其他人都有Visual Studio 2013专业版,虽然我怀疑它与问题无关。 请帮忙。 更新 __________ 更新到NUnit3测试适配器后没有错误,但仍未发现任何测试。 不知怎的,两个适配器都可用,但是使用Nuget和VS扩展我只能找到NUnit3测试适配器。 从https://visualstudiogallery.msdn.microsoft.com/0da0f6bd-9bb6-4ae3-87a8-537788622f2d安装NUnit3测试适配器

为整个项目全局声明预处理器符号(如DEBUG)

我想在NUnit和VS Tests之间切换,如下所示: #if !NUNIT using Microsoft.VisualStudio.TestTools.UnitTesting; #else using NUnit.Framework; using TestClass = NUnit.Framework.TestFixtureAttribute; using TestMethod = NUnit.Framework.TestAttribute; using TestInitialize = NUnit.Framework.SetUpAttribute; using TestCleanup = NUnit.Framework.TearDownAttribute; using TestContext = System.String; using DeploymentItem = NUnit.Framework.DescriptionAttribute; #endif 我的问题是,如何在一个地方声明NUNIT预处理器符号(App.config左右,会很棒),轻松地在NUnit和VSTests之间切换? 因为当我使用#define NUNIT ,它只适用于写入文件的文件。

NUnit:在TearDown中访问失败消息()

我正在尝试将NUnit中运行的自动化测试的结果记录在一个小型数据库中,以便数据易于访问并且由于各种原因更安全地记录。 (大约有550个自动化测试并运行它们都需要数天) 我已经可以访问测试的结束状态(Passed / Failed / Error / Cancelled / Skipped等..)但我想记录额外的细节。 我希望在TearDown()中做到这一点。 这是我能找到的最接近的东西,但没有给我答案: https : //groups.google.com/forum/?fromgroups =#! msg / nunit- discuss / lXxwECvpqFc / IbKOfQlbJe8J 想法?

使用NUnit Console Runner在文件夹下运行所有​​测试

我正在尝试使用NUnit Runners 2.6.4在我的测试文件夹中运行所有测试程序集。 我当前的命令如下所示: /nologo /noshadow /framework:net-4.0 /xml:.\test\TestResults.xml .\test\*.Test.dll 不幸的是,Nunit只抛出一个System.ArgumentException:路径中的非法字符。 无论如何我能做到这一点吗?