2016-03-08 70 views
1

我在設置測試時遇到問題。我已經嘗試使用控制檯的c#文件來運行測試,但是它並沒有出現在測試資源管理器中。當我創建一個單元測試c#項目時,它不會運行或顯示在測試資源管理器中。什麼做錯了?未在Visual Studio測試資源管理器中顯示的測試

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace SeleniumTests1 
{ 
    [TestClass] 
    class SeleniumTest 
    { 
     [TestMethod] 
     static void Main(string[] args) 
     { 

      IWebDriver driver = new FirefoxDriver(); 
      driver.Navigate().GoToUrl("http://www.bing.com/"); 
      driver.Manage().Window.Maximize(); 

      IWebElement searchInput = driver.FindElement(By.Id("sb_form_q")); 
      searchInput.SendKeys("Hello World"); 
      searchInput.SendKeys(Keys.Enter); 

      searchInput = driver.FindElement(By.Id("sb_form_q")); 
      string actualvalue = searchInput.GetAttribute("value"); 

      Assert.AreEqual(actualvalue, "Hello World"); 
      driver.Close(); 
     } 
    } 
} 
+0

您的測試方法通常應參考他們在其命名約定中測試的邏輯,例如testHelloWorldOnBing()等。然後每個測試類應該在一個測試項目中,然後可以單獨運行。 – ManoDestra

回答

0

您正在使Main方法成爲測試方法嗎? 製作一個單獨的測試項目,然後引用您正在測試的項目,並將代碼移至該項目,例如

namespace Tests 
{ 
    [TestClass] 
    public class MyProjTests 
    { 
     [TestMethod] 
     public void Test{ 
      //your code 
     } 
    } 
} 
+0

所以我創建了一個控制檯c#項目。在該項目中,我創建了一個新的c#測試項目,然後將主要方法引用到測試中?對不起,如果我聽起來啞巴。 – Peter

+0

因此,您製作控制檯應用程序,然後在該解決方案中創建另一個作爲測試項目的項目(右鍵單擊解決方案>添加>新項目>測試>單元測試,然後右鍵單擊該項目中的引用,然後單擊該窗口中的解決方案你會看到你的控制檯應用程序的名稱,單擊它,從單元測試類的頂部向該項目添加一個using,並準備開始你的單元測試 – LukeChristopher

+0

當我說控制檯應用程序時,我只是指一個你可能正在測試的東西的例子,在你的情況下,它將是你希望測試的代碼駐留在任何地方的參考 – LukeChristopher

2

這可能工作。我認爲你的TestMethod需要是public,並且它是非靜態的才能使它出現在測試瀏覽器中。

namespace SeleniumTests1 
{ 
    [TestClass] 
    public class SeleniumTest 
    { 
     [TestMethod] 
     public void Main() 
     { 
0

我想我可能已經設法解決這個問題,通過從selenium網站的net40文件導入4個dll。

相關問題