2016-01-22 79 views
0

對於我正在開發的一個新項目,我和我的團隊試圖使用ReSharper 10,NUnit,SpecFlow和Selenium設置一些功能測試。我們從一個已知的工作項目中遷移了一堆舊代碼,並且無需重新構建它。SpecFlow/NUnit/Selenium測試從錯誤的PWD運行

然而,當我們運行我們的代碼,我們不斷收到以下錯誤:

OneTimeSetUp: System.IO.DirectoryNotFoundException : Could not find a part of the path 'C:\Users\Me\AppData\Local\SomeApp\App_Data\SomeApp.db'. 

Exception doesn't have a stacktrace 

...這是不對的;如果無法找到SomeApp.db文件,則應該從C:\MyProjects\SomeApp\SomeApp.Web\App_Data\SomeApp.db開始失敗!

在這種情況下,相關的代碼是我們TestSuite.cs類:

[Binding] 
public class TestSuite 
{ 
    private string _currentPath; 
    private string _localFile; 
    private string _websiteBinPath; 
    private string _websiteDataPath; 

    // Stuff... 

    [BeforeTestRun] 
    public static void BeforeTestRun() 
    { 
     _currentPath = Directory.GetCurrentDirectory() + @"\"; 
     // Data: 
     // <add key="TestWebsiteDataRelativePath" value="..\..\..\SomeApp.Web\App_Data\" /> 
     // <add key="TestWebsiteBinRelativePath" value="..\..\..\SomeApp.Web\bin\" /> 
     _websiteBinPath = Path.Combine(_currentPath, ConfigurationManager.AppSettings["TestWebsiteBinRelativePath"]); 
     _websiteDataPath = Path.Combine(_currentPath, ConfigurationManager.AppSettings["TestWebsiteDataRelativePath"]); 

     //removes the ../../../ 
     _websiteBinPath = Path.GetFullPath((new Uri(_websiteBinPath)).LocalPath); 
     _websiteDataPath = Path.GetFullPath((new Uri(_websiteDataPath)).LocalPath); 

     _localFile = _websiteDataPath + "SomeApp.db"; 

     DoStuffWithLocalFile(); 
    } 

    // Stuff... 
} 

我們設下的代碼的第一個可執行行設置斷點,並跨過代碼,直到currentPath有一個值 - 該目錄currentPath曾經是C:\Users\Me\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\

我們已經通過ReSharper選項禁用了Shadow Copy,爲了額外的衡量,我還禁用了ReSharper選項中的MSTest支持。然而,該測試項目仍然不能運行在C:\MyProjects\SomeApp\SomeApp.FeatureTest\bin\Debug目錄之外。

問題: 以什麼方式才能讓我的功能測試項目,從C:\MyProjects\SomeApp\SomeApp.FeatureTest\bin\Debug運行,出來的C:\Users\Me\AppData\Local\JetBrains\Installations\ReSharperPlatformVs14\

回答

6

你使用NUnit 3.0嗎?如果是這樣,請參考NUnit 3.0 Breaking-Changes page

CurrentDirectory No longer set to the directory containing the test assembly. Use TestContext.TestDirectory to locate that directory.

+0

這就是我需要做的。僅供參考,上面的調用使用'System.IO.Directory'來獲得PWD,但這似乎是更好的方法。 –