IT박스

"테스트 디렉토리에 복사"를 사용하여 단위 테스트를 수행하려면 어떻게해야합니까?

itboxs 2020. 7. 10. 08:12
반응형

"테스트 디렉토리에 복사"를 사용하여 단위 테스트를 수행하려면 어떻게해야합니까?


테스트를 실행하기 전에 단위 테스트 프로젝트를 빌드하면 테스트 결과가 TestResults 폴더에 복사 된 다음 테스트가 실행됩니다. 내가 겪고있는 문제는 Debug / bin 디렉토리의 모든 파일이 TestResults 프로젝트에 복사되지 않는다는 것입니다.

Debug / bin 디렉토리로 복사 한 파일을 TestResults 폴더로 복사하려면 어떻게해야합니까?


이를 수행하는 표준 방법 파일에 배포 항목 을 지정하는 것입니다.이 항목은 Visual Studio 테스트 메뉴 또는 솔루션 항목 폴더의 테스트 실행 구성 편집 항목을 .testrunconfig통해 액세스 할 수 있습니다 .


아래에 표시된 예와 같이 배치 속성을 지정할 수 있습니다. 또한 "컨텐츠"및 "최신 경우 복사"속성을 설정해야합니다 (나중에 설정에 대한 설명서는 없지만 작동하도록 설정했습니다.

[TestMethod]
[DeploymentItem("mytestdata.xml")]
public void UploadTest()
{



}

속성이 작동 Test -> Edit Test Settings -> Local -> Deployment하려면 "배치 사용"을 설정 [DeploymentItem]해야했습니다.


필요에 따라 세 가지 답변이 모두 맞습니다.

.testrunconfig (VS2010의 .testsettings)에 배포 할 파일을 추가하면 관련없는 테스트가 단독으로 실행 되더라도 모든 파일이 모든 테스트 출력 폴더에 복사됩니다. 하나의 테스트를 실행하면 .testssettings의 배포 섹션에 나열된 모든 테스트 데이터 파일이 테스트 출력 폴더에 복사됩니다.

내 테스트에서 실제 테스트 출력 XML과 비교하기 위해 예상 XML 파일을 테스트 출력 폴더에 복사해야합니다. DeploymentItem 속성을 사용하여 실행중인 테스트와 관련된 XML 파일 만 복사합니다. VS2010에서는 .testsettings 파일에서 배포를 활성화해야하지만 경로를 추가하지 말고 DeploymentItem의 TestProject와 관련된 XML 파일 경로를 참조해야했습니다.

도움이 되었기를 바랍니다.


비슷한 문제가 있었지만 Local.testsettings 파일 대신 TraceAndTestImpact.testsettings 파일을 가리키는 것과 관련이 있습니다. Test / Select Active Test Settings 메뉴에서 하나에서 다른 것으로 변경할 수 있습니다.


다음은 테스트 설정 파일을 사용하지 않고 여러 솔루션에 포함 된 테스트 프로젝트에 대해 VS2012에서 작동합니다.

1) 배포하려는 파일과 폴더를 테스트 프로젝트 디렉토리의 폴더에 정렬하십시오.

2) 프로젝트 속성에서 빌드 후 단계 생성

xcopy /Y /S /i "$(ProjectDir)<Project_Folder_Name>\*" "$(TargetDir)<Deployment_Folder_Name>"

$(ProjectDir)그리고 $(TargetDir)VS 해석되며, 같은 포함되어야 매크로입니다.

<Project_Folder_Name> 1 단계에서 만든 폴더의 이름입니다.

<Deployment_Folder_Name>는 테스트 파일이 배포 될 폴더의 이름이며 여러 테스트 프로젝트가 동일한 디렉터리에 배포 될 때 고유하도록 이름을 지정해야합니다 (예 :) <Project_Name>_TestInputs.

공유 위치의 테스트 파일도 대상 상호 작용 배포 폴더에 복사하여 테스트 상호 작용을 제한해야합니다. $(ProjectDir)매크로를 기준으로 소스 경로를 제공하십시오 . 예를 들면 "$(ProjectDir)..\..\Common Files\C1219TDL-2008.xml".

3) [DeploymentItem(source, destination)]배포 파일을 사용하는 각 테스트 방법 (모범 사례) 또는 테스트 클래스 (게 으르거나 서두르 기 쉬운 방법 및 이전에 사용한 상대 경로 또는 테스트 설정 파일을 프로젝트를 업데이트하는 가장 쉬운 방법)에 속성을 추가합니다 ).

시험 방법에 source의해 생성 된 바와 같이 대상 디렉토리 시험 방법에 대하여 사용되는 파일이나 디렉토리의 경로 xcopydestination이 배치 디렉토리에 대해 생성 될 디렉토리 경로된다. 따라서 테스트는 대상 디렉토리 또는 배치 디렉토리에서 일관되게 실행됩니다. 대상 경로는 파일 참조가없는 소스 경로와 동일해야합니다. 예 : [DeploymentItem("Example_TestInputs\C1219TDL-2008.xml","Example_TestInputs")]. DeploymentItem해당 파일이나 디렉토리를 사용하는 모든 방법에 포함되어야한다.

On a class, source and destination are both the name of the folder created in the target directory by the xcopy; this will copy the entire folder to the deployment directory when any test in the class is run. Example: [DeploymentItem("Example_TestInputs","Example_TestInputs")]

4) In the test methods, you can now access files and directories with confidence they will be in the working directory regardless of where Visual Studio has decided to put it that day, e.g. File.Exists(".\Example_TestInputs\C1219TDL-2008.xml").


Would like to just enhance the accepted answer by mentioning a way to get it to deploy specifically for dll's rather then the normal method of using it for data or config etc, for the circumstances where CopyLocal doesn't work:

[DeploymentItem("bin\\release\\iRock.dll")]
[DeploymentItem("bin\\debug\\iRock.dll")]

Try out the Post-Build event command line from within Visual Studio (if you are using that IDE).


In Visual Studio 2012 you don't need a DeploymentItem attribute for the simple case. See my answer here


[TestMethod]
[DeploymentItem("ProjectName/Folder/SubFolder/file.xml", "Folder/Subfolder")]
public void YourTestMethod()
{
   // in the method you are testing you should have this:
   var filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + "/Folder/Subfolder/file.xml";
}

The accepted answer is correct and so are most of the other answers. However, over the years I have found that the Deploment system of Visual Studio Unit Tests using DeploymentAttribtue and Copy to Output to be cumbersome if you have a large number of data files. I have found that keeping the files in their original location worked better.

Full details in my other answer here. https://stackoverflow.com/a/53004985/2989655

Hope this helps.

참고URL : https://stackoverflow.com/questions/227545/how-can-i-get-copy-to-output-directory-to-work-with-unit-tests

반응형