1

クラスでは、テスト対象のプロジェクトと同じ名前空間にテスト フィクスチャを追加するように教えられました。例えば:

namespace Project
{
    class Decrypt : Cipher
    {
        public string Execute()
        {
            //Code here
        }
    }
    [TestFixture]
    {
        [Test]
        public void test1()
        {
            //Code here
        }
    }
}

私の大学のコンピューターの c# メニューに、「テスト」セクションがあることに気付きました (そこで実行することもできませんでした。方法がわかりません)。この古い 32b コンピューターにはありません。NUnit-2.6.2.msi をインストールしましたが、実行しようとすると、「このアプリケーションを実行するランタイムのバージョンが見つかりません」と表示されるので、2 つの問題があると思います。

  • Nunit のインストール (プロジェクトの .dll を個別に参照しています)

  • Nunit の使用 (適切にインストールされたコンピューター上でも)

4

1 に答える 1

2

通常、コードは別のプロジェクトに配置しますが、テスト プロジェクトでテストしているプロジェクトを参照します。

//project: Xarian.Security
//file: Decrypt.cs
namespace Xarian.Security
{
    class Decrypt : Cipher
    {
        public string Execute()
        {
            //Code here
        }
    }
}

.

//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");
        }
    }
}

テスト プロジェクトの参照を右クリックし、[プロジェクト] タブに移動してメイン プロジェクトを選択します。参照すると、メイン プロジェクトのクラス (など) をテスト コードで使用できるようになります。

于 2012-10-26T00:15:20.077 に答える