C# 如何将TestFixture与project放在同一个cs文件中?
在课堂上,他们教我们将测试夹具添加到与我们正在测试的项目相同的名称空间中。例如:C# 如何将TestFixture与project放在同一个cs文件中?,c#,unit-testing,nunit,C#,Unit Testing,Nunit,在课堂上,他们教我们将测试夹具添加到与我们正在测试的项目相同的名称空间中。例如: namespace Project { class Decrypt : Cipher { public string Execute() { //Code here } } [TestFixture] { [Test] public void test1()
namespace Project
{
class Decrypt : Cipher
{
public string Execute()
{
//Code here
}
}
[TestFixture]
{
[Test]
public void test1()
{
//Code here
}
}
}
我注意到在我的uni电脑的c菜单中,有一个“测试”部分,我也无法让它运行,我不知道如何运行。在这台旧的32b电脑上没有。我已经安装了NUnit-2.6.2.msi,但当我尝试运行它时,它说找不到运行此应用程序的运行时版本
所以我想我有两个问题:
安装Nunit我已经从我的项目中分别引用了.dll
即使在安装正确的计算机上使用Nunit
通常,您会将代码放在单独的项目中,但在测试项目中引用您正在测试的项目
//project: Xarian.Security
//file: Decrypt.cs
namespace Xarian.Security
{
class Decrypt : Cipher
{
public string Execute()
{
//Code here
}
}
}
右键单击测试项目的引用,转到“项目”选项卡并选择主项目。一旦被引用,您就可以在测试代码中使用主项目中的类等。我不会将测试放在与要测试的代码相同的文件或项目中。如果您使用resharper,它可以很好地集成运行测试。我不知道id VS会运行Nunit测试,但我想会的。
//project: Xarian.Security.Test
//file: DecryptTest.cs
using System;
using NUnit.Framework;
//as we're already in the Xarian.Security namespace, no need
//to reference it in code. However the DLL needs to be referenced
//(Solution Explorer, Xarian.Security.Test, References, right click,
//Add Reference, Projects, Xarian.Security)
namespace Xarian.Security
{
[TestFixture]
class DecryptTest
{
[Test]
public void test()
{
//Code here
Cipher cipher = new Decrypt("&^%&^&*&*()%%&**&&^%$^&$%^*^%&*(");
string result = cipher.Execute();
Assert.AreEqual(string, "I'm Decrypted Successfully");
}
}
}