program tip

테스트 실행 폴더에 MSTest 복사 파일

radiobox 2020. 8. 12. 08:06
반응형

테스트 실행 폴더에 MSTest 복사 파일


XML 파일을 읽고 파싱해야하는 테스트가 있습니다. 매번 테스트 실행 폴더에이 파일을 복사하려면 어떻게해야합니까?

XML 파일은 "Copy if newer"로 설정되고 컴파일 모드는 "none"으로 설정됩니다 (실제로 컴파일 할 수있는 것이 아니기 때문에).


속성 사용DeploymentItem

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CarMaker;

namespace DeploymentTest
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod()]
        [DeploymentItem("testFile1.xml")]
        public void ConstructorTest()
        {
            string file = "testFile1.xml";
            Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
                " did not get deployed");
        }
    }
}

솔루션에 대한 TestSettings 파일을 제공하면 "배포 활성화"옵션의 선택을 취소하고 mstest가 ...TestResults\...\out추가 파일을 복사하지 않는 폴더에서 실행 하지 못하도록 할 수 있습니다 (배포 옵션으로 지정하지 않는 한). .

TestSettings ()에서 배포, 폴더 추가 옵션을 사용하면 배포 항목이 모두 임시 실행 폴더 (외부)에 직접 (플랫) 복사되는 것처럼 보이기 때문에 보존 된 폴더 구조에있는 추가 파일에 의존하는 경우에도 유용합니다. 위의 답변은 각 항목을 자체 DeploymentItem으로 추가하면 구조를 유지할 수 있음을 제안합니다).

저에게는 오래 전에 다른 이유로 (배포 활성화가 선택되지 않은) TestSettings 파일을 만들었 기 때문에 Visual Studio에서 직접 테스트를 실행하는 것이 잘 작동했지만 (즉, 해당 구조의 내 추가 파일이 테스트에서 발견되어 사용되었습니다) TeamCity가 아닌 경우 TestSettings 파일을 사용해야한다고 지정하지 않았기 때문에 테스트를 실행하기 위해 mstest를 실행했습니다.

Visual Studio에서 TestSettings 파일을 만들려면 솔루션을 마우스 오른쪽 단추로 클릭 하고 새 항목을 선택한 다음 TestSettings 템플릿을 선택합니다. mstest.exe의 명령 프롬프트에서 TestSettings 파일을 사용하려면 옵션을 /testsettings:C:\Src\mySolution\myProject\local.testsettings추가하거나 적절한 경로를 사용하여 TeamCity에서 추가 명령 줄 옵션으로 추가하십시오.


Preet 답변은 단일 테스트에 대한 항목을 배포하는 데 사용됩니다. 솔루션 수준에서 수행하려면 .testrunconfig 설정을 사용하십시오 .


나에게 가장 좋은 해결책은 특히 여러 테스트에 동일한 데이터 파일이 필요한 경우 testsettings를 사용하는 것입니다.

먼저 testsettings 파일을 만들고 필요한 배포 항목 (파일 또는 폴더 이름)을 추가합니다.

<TestSettings name="Local" id="00ebe0c6-7b64-49c0-80a5-09796270f111" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>These are default test settings for a local test run.</Description>
  <Deployment>
    <DeploymentItem filename="Folder1\TestScripts\test.xml" outputDirectory="TestScripts"/>
    <DeploymentItem filename="Folder2\TestData\" outputDirectory="TestData"/>
  </Deployment>
<...../>
  • Visual Studio에서 실행하고 "테스트 \ 테스트 설정"메뉴에서 "테스트 설정 파일 선택"을 사용하여 새 테스트 설정을 선택합니다.

  • Running mstest, use the /testsettings parameter to have mstest use your testsettings.


You can define DeploymentItem in a class that holds a method with AssemblyInitialize attribute. Then you're sure that the files are copied regardless of which test you run.

Unfortunately DeploymentItem attribute is executed only on classes which contain tests you're running. So if you have 10 test classes which use the same set of files, you have to add the attribute to all of them.

Also found out that changes in *.testsettings files are not automatically refreshed in Visual Studio. Therefore after adding files / folders into deployment in testsettings, you have to reopen solution file and then run the tests.


In Visual Studio 2012, vstest.console.exe (the built-in test runner) runs with the output dir as the current path. This means that you only need to include the items in your solution with the 'Copy always' or 'Copy if newer' property for them to be used by your test. You don't need the DeploymentItem attribute for the general case. The same applies when running vstest.console.exe from the command line inside your output/test directory.

There are some cases where a separate folder is used, one of them being when you are using the DeploymentItem attribute. See here for more information.

참고URL : https://stackoverflow.com/questions/649106/mstest-copy-file-to-test-run-folder

반응형