24

目標は、それらのXmlファイルにいくつかのデータを指定していくつかのテストを実行することです。

特定のXmlファイルを単体テストメソッド内のXmlDocに簡単にロードするにはどうすればよいですか?

現在の状態は次のとおりです。

  XmlDocument doc = new XmlDocument();
  string xmlFile = "4.xml";
  string dir = System.IO.Directory.GetCurrentDirectory() + @"\Msgs\" 

  //dir is then the value of the current exe's path, which is
  //d:\sourcecode\myproject\TestResults\myComputer 2009-10-08 16_07_45\Out

  //we actually need:
  //d:\sourcecode\myproject\Msgs\ 
  doc.Load( dir + fileName); //should really use System.IO.Path.Combine()!

そのパスを?に入れるだけの簡単な問題app.configですか?開発者のマシンで異なるパスが発生する可能性があることを考えると、私はそれを避けたいと思っていました。

質問:単体テストメソッドで特定のXmlファイルをXmlDocumentにロードするアルゴリズムをどのように記述しますか?

4

7 に答える 7

26

There is a Visual Studio Unit Testing feature for this: DeploymentItemAttribute

I use this feature to copy all xml files in a given project folder to the unit test output folder, before testing if all required files are present.

You can use this attribute with your unit tests to copy specific files from the Project folder (or anywhere else) to the Unit Test output folder. Like so:

[TestMethod()]
[DeploymentItem("MyProjectFolder\\SomeDataFolder\\somefile.txt", "SomeOutputSubdirectory")]
public void FindResourcefile_Test()
{
    string fileName = "SomeOutputSubdirectory\\somefile.txt";
    Assert.IsTrue(System.IO.File.Exists(fileName));
}

You can also copy the contents of whole folders:

[TestMethod()]
[DeploymentItem("MyProjectFolder\\SomeDataFolder\\", "SomeOutputSubdirectory")]
public void FindResourcefile_Test()
{
    string fileName = "SomeOutputSubdirectory\\someOtherFile.txt";
    Assert.IsTrue(System.IO.File.Exists(fileName));
}

The first parameter is the source, the second the destination folder. The source is relative to your solution folder (so you can access the Unit Test project of the project being tested) and the destination is relative to the output folder of the unit test assembly.

UPDATE:

You need to enable Deployment in the Test Settings for this to work. This MSDN page explains how (it's real easy): http://msdn.microsoft.com/en-us/library/ms182475(v=vs.90).aspx#EnableDisableDeploy

于 2012-07-25T11:35:52.553 に答える
24

これらのファイルを実行可能ファイルにビルドし (「ビルド アクション」プロパティを「埋め込みリソース」に設定) Assembly.GetManifestResourceStream method、.

于 2009-10-08T23:44:41.253 に答える
11

単体テスト プロジェクトで、XML ファイルを出力ディレクトリにコピーするビルド後のイベントを追加します。その後、元のコードを使用して XML ファイルを取得できます。

ビルド後のイベントは次のようになります。

copy $(SolutionDir)file.xml $(ProjectDir)$(OutDir)file.xml

これをパスに追加する必要がある場合もあります。

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
于 2009-10-08T23:54:08.090 に答える
5

ヘルパー クラスを使用して、単体テストでアクセスする可能性のある基本的なパスを取得します。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Brass9.Testing
{
    public static class TestHelper
    {
        public static string GetBinPath()
        {
            return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        }

        public static string GetProjectPath()
        {
            string appRoot = GetBinPath();
            var dir = new DirectoryInfo(appRoot).Parent.Parent.Parent;
            var name = dir.Name;
            return dir.FullName + @"\" + name + @"\";
        }

        public static string GetTestProjectPath()
        {
            string appRoot = GetBinPath();
            var dir = new DirectoryInfo(appRoot).Parent.Parent;
            return dir.FullName + @"\";
        }

        public static string GetMainProjectPath()
        {
            string testProjectPath = GetTestProjectPath();
            // Just hope it ends in the standard .Tests, lop it off, done.
            string path = testProjectPath.Substring(0, testProjectPath.Length - 7) + @"\";
            return path;
        }
    }
}

パスとのやり取りがより複雑になることがあります。私はよく「App」という名前の中央クラスを使用して、アプリケーションの基本的な詳細 (ルート フォルダー、ルート名前空間、モジュールなど) を示します。クラスは App の存在に依存する場合があるため、代わりに init を配置します。上記のようなコードを使用してテスト ハーネス用に自身を初期化し、単体テストで Init コマンドからそのメソッドを呼び出す App のメソッド。

(更新しました)

古い回答

これは、テストしようとしているプロジェクト フォルダー内のファイルにアクセスするための任意のパスを取得するのに役立つことがわかりました (テスト プロジェクト フォルダー内のファイルとは対照的に、何かをコピーする必要がある場合に面倒な作業になる可能性があります)。

DirectoryInfo projectDir = new DirectoryInfo(@"..\..\..\ProjectName");
string projectDirPath = projectDir.FullName;

これらの変数のいずれかを使用して、関連するプロジェクトから必要なものにアクセスできます。明らかに、「ProjectName」をプロジェクトの実際の名前に置き換えます。

于 2011-03-30T00:28:25.640 に答える
1

リソースは単なるリソースであり、複雑にする必要はありません。それらを埋め込みたくない場合は、これらのファイルを「コンテンツ」リソースとしてプロジェクトに追加し、Copy always. 次に、コードでサブフォルダーを指定します。

var xmlDoc = XElement.Load("ProjectSubFolder\\Resource.xml");

これにより、プロジェクト出力 (実行中のアセンブリの場所) からリソースが自動的に読み込まれます。bin\$(Configuration)\ResourceSubfolder\

これは、単体テストだけでなく、すべてのタイプのプロジェクトで機能します。

于 2014-04-09T23:03:30.630 に答える
0

パスをapp.configに入れて、デフォルトのパスからロードします。私のチームでは、開発者がパスを変更することについて本当に肛門的です。したがって、すべての開発者が自分のコンピューター上で同じ正確なパスとファイルを持つようにします。したがって、不正な開発者が自分のワークスペースに合わせてパスを変更するという問題はありません。

たとえば、私のチームのすべての開発者は、C:\ Project \ Product \ Moduleなどを使用する必要があります。また、インストールされているすべてのソフトウェアも標準であることを確認します。このようにして、どのマシンも他のマシンに簡単にゴースト化できます。

于 2009-10-08T23:37:52.603 に答える