2011-09-25 1 views
3

Excel에서 .xlsx 데이터 소스를 사용하도록 설정 테스트를하는 데 문제가 있습니다.데이터 구동 유닛 테스트 문제

내 app.config 파일은 :

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 
    </configSections> 
    <connectionStrings> 
    <add name="TestData" connectionString="Dsn=Excel Files;dbq=TestData.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" /> 
    </connectionStrings> 
    <microsoft.visualstudio.testtools> 
    <dataSources> 
     <add name="GetAllCellNamesTest" connectionString="TestData" dataTableName="GetAllCellNamesTest$" dataAccessMethod="Sequential"/> 
</dataSources> 

나는 TestData.xlsx을 찾는 것을 확인했으며, GetAllCellNamesTest라는 이름의 시트가있다.

 [TestMethod()] 
     [DeploymentItem("TestProject\\TestData.xlsx")] 
     [DataSource("GetAllCellNamesTest")] 
     public void GetAllCellNamesTest() 
     { 
      // ... test code 

TestData.xlsx이 테스트 결과 디렉토리 및 전달하는 데이터 소스를 참조하려고하지 않는 모든 단위 테스트에 복사되는 : 내 단위 테스트 클래스에서

, 나는 다음과 같은 설정이있다.

그러나,이 테스트는 다음 메시지와 함께 실패 : Walkthrough: Using a Configuration File to Define a Data Source :

The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library. 
Error details: ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Access database engine could not find the object 'GetAllCellNamesTest$'. Make sure the object exists and that you spell its name and the path name correctly. If 'GetAllCellNamesTest$' is not a local object, check your network connection or contact the server administrator. 

난 정말 내 설정이 잘못에서, 내가 설정을 얻기 위해 MSDN에이 연습을 따라 어디 모르겠어요. 유의하십시오. section 버전을 10.0.0.0으로 변경했는데, 이는 .net 4.0 (페이지 하단의 참고 사항)을 사용하고 있기 때문입니다.

편집 : 오, 모든 파일이 내 컴퓨터에 로컬로 있습니다.

+0

이것은 내가 MSTest보다 NUnit 및 xUnit를 선호하는 이유 중 하나입니다. [TestCase] ​​또는 [Theory]를 사용하여 코드로 모두 작업하는 것이 더 편리합니다. – Mathias

답변

3

전체 파일 경로를 사용해 보셨습니까?

파일이 읽기 전용입니까?

+0

아, 고마워. 전체 경로 사용이 효과적입니다. 그러나 프로젝트가 Subversion repo에 있기 때문에 상대 경로를 사용하고 싶습니다. 검색 할 기본 디렉토리가 무엇인지 알고 있습니까? – helloworld922

+0

내 생각 엔 테스트에 bin 디렉토리가 부족한 것 같습니다. \ bin \ debug로 가정하면, 아마도'.. \ .. TestData.xlsx'의 속성을 시도해보십시오. –

+0

이것을 콘솔이나 파일에 빠르게 작성하십시오. 'DirectoryInfo ApplicationRoot = 새로운 DirectoryInfo (Environment.CurrentDirectory); '내 테스트 프로젝트에서 나는 프로젝트 루트를 찾기 위해'DirectoryInfo ApplicationRoot = new DirectoryInfo (Environment.CurrentDirectory) .Parent.Parent;'를 사용하고있다. –

2

TestSettings의 일부로 배포 할 파일/디렉터리를 지정할 수도 있습니다. 이렇게하면 모든 테스트 메서드에 대해 DeploymentItem 특성을 넣지 않아도됩니다.

1

구성을 사용합니다.

<configSections> 
    <section name="microsoft.visualstudio.testtools" 
      type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, 
      Microsoft.VisualStudio.QualityTools.UnitTestFramework, 
      Version=10.0.0.0, Culture=neutral, 
      PublicKeyToken=b03f5f7f11d50a3a"/> 
</configSections>