Tag: vs unit testing framework

数据驱动的MSTest:DataRow始终为空

我在使用Visual Studio数据驱动测试时遇到问题。 我试图将其解构为最简单的例子。 我正在使用Visual Studio 2012.我创建了一个新的unit testing项目。 我正在引用系统数据。 我的代码如下所示: namespace UnitTestProject1 { [TestClass] public class UnitTest1 { [DeploymentItem(@”OrderService.csv”)] [DataSource(“Microsoft.VisualStudio.TestTools.DataSource.CSV”, “OrderService.csv”, “OrderService#csv”, DataAccessMethod.Sequential)] [TestMethod] public void TestMethod1() { try { Debug.WriteLine(TestContext.DataRow[“ID”]); } catch (Exception ex) { Assert.Fail(); } } public TestContext TestContext { get; set; } } } 我有一个非常小的csv文件,我已将“构建选项”设置为“内容”和“始终复制”。 我在解决方案中添加了.testsettings文件,并设置了enable deployment,并添加了csv文件。 我有没有|DataDirectory|尝试过这个 ,有/没有指定完整路径(与Environment.CurrentDirectory相同的路径)。 为了以防万一,我尝试了“../”和“../../”的变体。 现在,csv位于项目根级别,与.cs测试代码文件相同。 我尝试过使用xml和csv的变体。 […]

找出要在MS TestInitialize中执行的下一个测试方法

我将特定测试方法的测试数据保存在与函数相同的文件夹中。 我以前在每个[TestMethod] , ClearAllAndLoadTestMethodData()中都有相同的函数调用,它通过StackTrace确定方法名称。 现在,我将此函数移至[TestInitialize] 。 如何找到即将执行的方法的名称? 我以为TestContext提供了这个。 我可以通过[AssemblyInitialize()]访问它,并在第一次运行时将其属性Name设置为testmethod的名称。 但是,稍后这不会改变(如果我将对象保存在静态字段中)。

Visual Studio C#unit testing – 使用各种/多个测试初始化​​运行unit testing,多次运行相同的unit testing?

我想要做的是: 创建一堆unit testing。 创建各种不同的模拟初始化/组合,输入变量等。 基于某些参数,针对一组此类初始化运行每个给定的unit testing。 我该怎么做这样的事情? 是否已经有任何框架来处理这个问题(即在更改初始化时多次运行给定的测试)? 你能否提出任何我可以做到这一点的设计或想法? 我知道unit testing框架的工作原理。 我自己使用NUnit和Rhino嘲笑。 下面显示的是我需要的一个例子。 [Test Initialize] Setup( ) [Test Method] TestA() 现在我希望TestA()多次运行。 每次Test initialize都会选择另一个初始化组合。 更多澄清 让我们假设测试需要变量A,B,C。它们中的每一个都是非常复杂的对象,最终结果是可以形成大量的组合。 所以我希望以某种方式我可以创建一个测试初始化​​,可以迭代这样的组合列表,所以它会初始化它们,运行TESTA,返回列表中的下一个初始化,再次运行TESTA等等,直到列表用完了。 接下来,它为TESTB选择另一个列表,然后再次执行此过程。 至少我希望有一些能力能够运行给定的TESTfunctionn次。 其余的我知道一旦这是可能的,我可以建立

如何在RunSettings文件中访问TestRunParameters

通过https://msdn.microsoft.com/en-us/library/jj635153.aspx阅读我创建了一个.RunSettings文件,其中包含一些类似于示例的参数: 我计划为每个环境提供一个.RunSettings文件,其中包含适当的URL和凭据,用于在指定的RunSettings文件环境中运行CodedUI测试。 我可以看到从命令行引用我可以运行的设置文件: vstest.console myTestDll.dll /Settings:Local.RunSettings /Logger:trx vstest.console myTestDll.dll /Settings:QA.RunSettings /Logger:trx 等等… 但我没有看到任何方式来调用如何在codedUI测试中实际使用TestRunParameters。 我想要做的是设置测试初始化​​程序,使用TestRunParameters来确定登录的位置以及要使用的凭据。 像这样的东西: [TestInitialize()] public void MyTestInitialize() { // I’m unsure how to grab the RunSettings.TestRunParameters below string entryUrl = “”; // TestRunParameters.webAppUrl string userName = “”; // TestRunParameters.webAppUserName string password = “”; // TestRunParameters.webAppPassword LoginToPage(entryUrl, userName, password); } public void LoginToPage(string entryUrl, […]

StreamWriter.WriteLine()无法正常工作

我试图使用StreamWriter.WriteLine(非静态)将几行(一次一行)写入.txt文件。 const string filename = “BasicTestInfo.txt”; using (var writer = new StreamWriter(filename, false)) { writer.WriteLine(“{0} 350 200 200 10 2 28 20 200 2500 1200 1 1”, Player1); writer.WriteLine(“{0} 300 150 150 4 2 15 18 150 2500 1000 1 0”, Player2); writer.WriteLine(“{0} 200 140 450 25 14 10 70 4500 2500 750 1 1”, […]

DeploymentItem不部署文件

我正在使用MS unit testing framework来测试我的C#库。 我必须使用DeploymentItem attribute打开我正在部署的文件。 但它没有将文件部署到Test部署目录。 在我的Unit-test项目中,我在这个文件夹中创建了一个文件夹TestFile ,有多个文件,比方说a.txt,b.txt。 现在在我的Unit-test类中,我添加了DeploymentItem属性。 但文件没有被复制。 这是我的代码。 [TestClass] [DeploymentItem(“TestFile”)] public class CustomLibraryTest { public TestContext TestContext { get; set; } [TestMethod] [DeploymentItem(@”TestFiles\a.txt”)] // THis should deploy a.txt to test deployment directory { var path = TestContext.TestDeploymentDir + “a.txt”; // Now when I debug this unit-test, and open path in explorer, […]

MSTest无法找到assembly体

我正在使用MSTest 我使用命令mstest /testsettings:local.Testsetting /testcontainer:folder\obj\Debug\test.dll 这是输出, 运行具有以下问题:警告:测试运行部署问题:程序集或模块’Microsoft.Practices。 未找到由测试容器’test.dll’直接或间接引用的Prism。 警告:测试运行部署问题:未找到测试容器“test.dll”直接或间接引用的程序集或模块“Project.Common.dll”。 警告:测试运行部署问题:未找到测试容器“test.dll”直接或间接引用的程序集或模块“Project.Infrastructure.dll”。 警告:测试运行部署问题:程序集或模块’Microsoft.Practices。 未找到由测试容器’test.dll’直接或间接引用的Prism。 我能做些什么让MSTest运行良好。

什么可能导致Visual Studiounit testing中的System.TypeLoadException?

我有一个编译好的C#.NET类库MyClassLibrary 。 我正在尝试为它创建一个unit testing项目(使用Visual Studiounit testing框架,使用Visual Studio 2010)。 类库中确实有大类,但每当我对最简单的类运行最简单的测试时,我会得到以下exception: 测试方法MyClassLibraryTest.MyClassLibraryTests.MySimpleClassTest抛出exception:System.TypeLoadException:无法从程序集“MyClassLibrary,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”加载类型“MyClassLibrary.MySimpleClass”。 我正在处理的所有项目都在同一个解决方案中,所有项目都是针对.NET 4.0编译的。 所有这些都在Windows 7 64位计算机上。 这是奇怪的部分:当我“ 运行 ”测试时,我得到上述错误。 但是当我“ 调试 ”测试时,它运行正常。 为什么?

unit testing中的模型状态validation

我正在为这样的控制器编写unit testing: public HttpResponseMessage PostLogin(LoginModel model) { if (!ModelState.IsValid) return new HttpResponseMessage(HttpStatusCode.BadRequest); } 该模型看起来像: public class LoginModel { [Required] public string Username { set; get; } [Required] public string Password { set; get; } } 然后我有像这样的unit testing: [TestMethod] public void TestLogin_InvalidModel() { AccountController controller = CreateAccountController(); … var response = controller.PostLogin(new LoginModel() { }); Assert.AreEqual(HttpStatusCode.BadRequest, […]