簡體   English   中英

如何檢測是否在TeamCity中運行NUnit測試?

[英]How can I detect if an NUnit test is running from within TeamCity?

只有當我在TeamCity測試啟動器內運行時,我才需要運行一些代碼。 檢測這種情況最簡單的方法是什么?

檢查是否定義了TEAMCITY_VERSION環境變量。

另一種方法是使用NUnit類別。

根據下面的評論,此代碼應該能夠檢查teamcity是否正在運行測試:

private static bool IsOnTeamCity() 
{ 
    string environmentVariableValue = Environment.GetEnvironmentVariable("TEAMCITY_VERSION"); 
    if (!string.IsNullOrEmpty(environmentVariableValue)) 
    { 
         return true; 
    } 
    return false; 
} 

我基本上是用以下屬性做的。 它通過調用程序集的代碼庫獲取目錄名稱,如果它包含TeamCity構建代理程序目錄的一部分,則它在TeamCity中運行。

public static bool IsTeamCity
{
    get
    {
        // the Assembly.GetExecutingAssembly().Location property gives funny results when using 
        // NUnit (where assemblies run from a temporary folder), so the use of CodeBase is preferred.
        string codeBase = Assembly.GetCallingAssembly().CodeBase;
        string assemblyFullPath = Uri.UnescapeDataString(new UriBuilder(codeBase).Path);
        string assemblyDirectory = Path.GetDirectoryName(assemblyFullPath);

        // a full TeamCity build directory would be e.g. 'D:\TeamCity\buildAgent\work\de796548775cea8e\build\Compile'
        return assemblyDirectory.ToLowerInvariant().Contains("buildagent\\work");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM