Tag: mstest

Assert.Equals的用法是什么?

我正在为我当前的项目进行unit testing,并遇到了一些奇怪的事情。 .Net UnitTesting库同时具有Assert.Equals和Assert.AreEqual。 Assert.Equals的备注说使用Assert.AreEqual比较两个对象,但没有说明为什么要对Assert.Equals这样做。 有人可以解释你何时应该在unit testing中使用Assert.Equals,如果有的话,以及Assert.Equals和Assert.AreEqual之间的区别?

NUnit在程序集中找不到测试

我使用MSTestinheritance了一个程序集,但是这些测试是在构建机器上使用nunit-console运行的(不确定它是如何工作的)。 因此我决定将其排序并将其更改为正确的NUnit测试,但现在nunit-console(或gui)找不到任何测试。 它们使用ReSharper测试运行器运行得很好。 知道可能遗漏的是什么吗?

我应该安装哪个版本的VS以允许在构建服务器上进行unit testing?

几个星期后,我们有了第一个TFS(2013)设置。 设置相当小,只有少数(读取:10)开发人员暂时不得不使用它。 它被设置为“测试”,以确定它是否适合我们的组织。 现在,我们还想测试构建服务器function。 我们已经配置了构建服务器并启用了单个代理。 然后,MSDN声明“您必须在构建代理上安装您的团队在其开发机器上使用的Visual Studio版本。” ( http://msdn.microsoft.com/en-us/library/bb399135.aspx )。 这听起来非常简单,只需安装我们用于开发的Visual Studio版本(VS Premium 2013)。 然而,我们对可以发放的许可证数量非常紧张。 那么VS Express就足够了吗? 或者VS Premium是否真的需要进行自动化unit testing(MS测试,而不是NUnit)? 如果需要VS Premium,应该使用哪个许可证来激活它? 通过“哪个许可证”我的意思是,开发人员MSDN许可证会起作用吗? 我们有一个silverlight的MS合作伙伴关系,每个开发者都有自己的MSDN订阅者帐户。 免责声明:这是我的第一个问题,如果我违反了任何规则,请原谅。

Assert.AreEqual失败了,但它不应该失败

我有一个非常奇怪的行为,我无法解释。 我有以下课程: public class Project { public virtual int Id { get; set; } public virtual string Name { get; set; } } 还有一个返回Project对象的方法: public Project GetByName(string Name) { using (ISession session = NHibernateHelper.OpenSession()) { Project project = session.CreateCriteria(typeof(Project)) .Add(Restrictions.Eq(“Name”, Name)) .UniqueResult(); return project; } } 我添加了一个unit testing来测试GetByName方法: [TestMethod] public void TestGetByName() { IProjectsRepository projectRepository […]

从Visual Studio 2015中的代码覆盖率中排除自动属性

我刚刚将一堆项目升级到VS2015 / C#6。 现在,MSTest的代码覆盖率分析报告unit testing未涵盖某些自动属性。 在Visual Studio 2013中并非如此,我怀疑它可能与C#6中的新autopropertyfunction有关。 处理所产生的所有误报都会使代码覆盖率工具的目的失败,因为它几乎不可能识别缺乏测试覆盖率的实际代码。 我们不想为所有DTO编写unit testing,而且我真的不需要通过ExcludeFromCodeCoverage为项目注释每个单独的自动属性。 我在https://github.com/iaingalloway/VisualStudioCodeCoverageIssue创建了一个有效的MCVE 在Visual Studio 2013 Premium或Ultimate中打开VisualStudio2013.sln 。 单击测试 – >分析代码覆盖 – >所有测试。 观察“代码覆盖率结果”窗口报告0块“未涵盖”。 在Visual Studio 2015 Enterprise中打开VisualStudio2015.sln 。 单击测试 – >分析代码覆盖 – >所有测试。 观察“代码覆盖率结果”窗口报告1块“未覆盖”(ExampleDto.Value的getter) 是否可以在Visual Studio 2015中配置内置代码覆盖率工具以忽略Visual Studio 2013之类的自动属性?

MSTest测试上下文exception处理

有没有办法可以使用TestContext或基础测试类上的其他方法来处理由MSTest框架处理的exception? 如果在我的一个测试中发生未处理的exception,我想旋转exception.Data字典中的所有项目并将它们显示给测试结果,以帮助我找出测试失败的原因(我们通常会将数据添加到帮助我们在生产环境中调试的exception,所以我想做同样的测试)。 注意:我没有测试exception是支持HAPPEN(我有其他测试),我正在测试一个有效的情况,我只需要查看exception数据。 这是我正在谈论的代码示例。 [TestMethod] public void IsFinanceDeadlineDateValid() { var target = new BusinessObject(); SetupBusinessObject(target); //How can I capture this in the text context so I can display all the data //in the exception in the test result… var expected = 100; try { Assert.AreEqual(expected, target.PerformSomeCalculationThatMayDivideByZero()); } catch (Exception ex) { ex.Data.Add(“SomethingImportant”, “I want to […]

了解MSTest TestContext

使用MSTest,我需要从[TestInitialize]方法中获取当前测试的名称。 您可以从TestContext.TestName属性获取此信息。 我发现传入[ClassInitialize]方法的静态TestContext与声明为公共属性的静态TestContext (由测试运行器设置)之间存在意外差异。 请考虑以下代码: using System; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace TestContext.Tests { [TestClass] public class UnitTest1 { public TestContext TestContext { get; set; } private static TestContext _testContext; [ClassInitialize] public static void SetupTests(TestContext testContext) { _testContext = testContext; } [TestInitialize] public void SetupTest() { Console.WriteLine( “TextContext.TestName='{0}’ static _testContext.TestName='{1}'”, TestContext.TestName, _testContext.TestName); } [TestMethod] public void TestMethod1() […]

如何将同步上下文添加到异步测试方法

我有Visual Studio 2012和需要同步上下文的异步测试。 但是MSTest的默认同步上下文为null。 我想测试在具有同步上下文的WPF或WinForms-UI线程上运行。 将SynchronizationContext添加到测试线程的最佳方法是什么? [TestMethod] public async Task MyTest() { Assert.IsNotNull( SynchronizationContext.Current ); await MyTestAsync(); DoSomethingOnTheSameThread(); }

安装了Selenium WebDriver.ChromeDriver Nuget包,但不适用于MSTest

我已将WebDriver.ChromeDriver nuget包添加到我的解决方案中,该解决方案包含Selenium WebDriver使用Chrome运行自动化测试所需的ChromeDriver.exe文件。 查看包内容,它只包含以下文件: tools\chromedriver.exe 这应该做的是将此文件夹添加到PATH环境变量,以便可以通过以下代码访问chromedriver.exe (这是在使用MSTest的UnitTest项目中): [TestMethod] public void LaunchWebsite_Chrome() { // create ChromeDriver – this should work if chromedriver.exe // is known to the environment PATH variable IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl(“http://localhost/”); } 但是,我仍然遇到以下exception: chromedriver.exe文件不存在于当前目录或PATH环境变量的目录中。 该驱动程序可以从http://code.google.com/p/chromium/downloads/list下载。 查看nuget文档,它表明nuget包的tools文件夹中的任何内容都将自动添加到PATH环境变量中: http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package :软件包的tools文件夹用于可从软件包管理器控制台访问的powershell脚本和程序。 将文件夹复制到目标项目后,它将添加到`$ env:Path(PATH)环境变量中。 但是,这似乎不起作用。 我甚至在命令行上运行了echo %PATH% ,它没有将我的工具文件夹显示为已注册。 我究竟做错了什么?

MSTEST PrincipalPermission

如何对使用PrincipalPermission属性修饰的代码进行unit testing? 例如,这有效: class Program { static void Main(string[] args) { AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); var c = new MyClass(); } } [PrincipalPermission(SecurityAction.Demand, Role = @”BUILTIN\Users”)] class MyClass { public MyClass() { Console.WriteLine(“This works.”); } } 这会抛出SecurityException: [TestClass] public class UnitTest1 { [TestInitialize] public void TestInitialize() { AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); } [TestMethod] public void TestMethod1() { var c = new […]