Google Test Adapter

平常 C++ 的開發工具是 Microsoft Visual Studio,然後現在的測試框架是使用 Google Test,以前都是一邊寫,一邊手動執行 test case 來驗證,沒有辦法跟 VS 內建的 Test Explorer 做整合,真的蠻原始的 =.=
最近因為 TDD 的關係,這樣子的開發環境真的太鳥了,所以認真研究了一下解決方案,發現了 Google Test Adapter 這個好東西:
https://github.com/csoltenborn/GoogleTestAdapter



很簡單,照著 github 上的說明安裝完後就可以使用。幾個要注意調整的地方:
  1. 原本它會搜尋結尾為 test / tests 的執行檔來分析裡面的 test case,所以如果你的 Unit Test 程式不是   xxxTest.exe 的話,請記得到 [TOOLS] –> [Option] –> [Google Test Adapter] –> [General] ,裡面有一個 Regex for test discovery,我是使用 UT_[\w]*.exe
  2. 在同一頁中還有一個 Working directory & PATH extension,如果你的執行環境不是在預設的 output directory,也請記得修改。
這樣子基本上就可以跑了,而且只要程式碼有重新編譯,它就會自動幫你跑一次 test case(Run Tests After Build 記得勾起來) 但還有幾個小問題:
  1. 修改 Unit Test Project 會自動跑 test case,但修改原本的 project 並不會被偵測到
  2. 希望可以每次存檔後,就自動 Build & Test ,這樣只要專注綠燈/紅燈就好了。
這個問題我是參考 http://stackoverflow.com/questions/33234498/automatically-build-after-save-for-visual-studio-2015,用 Visual Commander 來解決:多加一個 Visual Commander Extension

public class E : VisualCommanderExt.IExtension
{
    public void SetSite(EnvDTE80.DTE2 DTE_, Microsoft.VisualStudio.Shell.Package package)
    {
        DTE = DTE_;
        events = DTE.Events;
        documentEvents = events.DocumentEvents;
        documentEvents.DocumentSaved += OnDocumentSaved;
    }
    public void Close()
    {
        documentEvents.DocumentSaved -= OnDocumentSaved;
    }
    private void OnDocumentSaved(EnvDTE.Document doc)
    {
        if(doc.Language == "C/C++")
        {
            DTE.ExecuteCommand("Build.BuildSolution");
            DTE.ExecuteCommand("Test.RunAllTestsInSolution");
        }
    }
    private EnvDTE80.DTE2 DTE;
    private EnvDTE.Events events;
    private EnvDTE.DocumentEvents documentEvents;
}

完工,現在只要存檔,就會自動 Build,並且在 Test Explorer 看到紅燈綠燈了



12 comments

  1. The Casino Del Sol, San Diego, CA, USA
    Find The Casino Del Sol, San Diego, CA, USA news, weather, 서귀포 출장안마 and 과천 출장마사지 the latest updates. 대구광역 출장안마 Get the latest Breaking News, Weather and 부산광역 출장마사지 the latest times 남원 출장마사지 to

    ReplyDelete
  2. Right here is the perfect blog for everyone who would like to understand this topic.
    You understand so much its almost hard to argue with you You certainly put a fresh spin on a subject which has been written about for a long time. Excellent stuff, just excellent!

    토토 파워볼
    토토사이트 카지노

    ReplyDelete
  3. Very informative idea.i want to invite you to visit my site please try it.thank you so much Feel free to visit my website;

    ReplyDelete
  4. Wonderful article! That is the kind of information that are meant to be shared around the net.

    ReplyDelete
  5. These are actually great ideas in regarding blogging.

    ReplyDelete
  6. You have touched some pleasant factors here.

    ReplyDelete
  7. Hey there, You’ve done an incredible job. keep it up! Beautiful story you make

    ReplyDelete
  8. Great post! We will be linking to this great post on our website. Keep up the great writing.

    ReplyDelete
  9. I found some useful topics in this well detail form. Thanks. Good job you did it!

    ReplyDelete
  10. I believe that this weblog is interesting. Thank you for your time and efforts here.

    ReplyDelete