如何检测是否在TeamCity中运行NUnit测试?

时间:2022-04-03 23:15:35

I need to run some code only if I'm running from within the TeamCity test launcher. What's the easiest way to detect this?

只有当我在TeamCity测试启动器中运行时,我才需要运行一些代码。检测这种情况最简单的方法是什么?

2 个解决方案

#1


18  

Check if TEAMCITY_VERSION environment variable is defined.

检查是否定义了TEAMCITY_VERSION环境变量。

Another approach is to use NUnit categories.

另一种方法是使用NUnit类别。

Based on the comment below this code should be able to check if the test is being run by teamcity:

根据下面的注释,此代码应该能够检查teamcity是否正在运行测试:

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

#2


2  

I'm basically doing that with the following property. It get's the directory name via code base of the calling assembly and if it contains parts of your TeamCity build agent directory it is running within TeamCity.

我基本上是用以下属性做的。它通过调用程序集的代码库获取目录名称,如果它包含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");
    }
}

#1


18  

Check if TEAMCITY_VERSION environment variable is defined.

检查是否定义了TEAMCITY_VERSION环境变量。

Another approach is to use NUnit categories.

另一种方法是使用NUnit类别。

Based on the comment below this code should be able to check if the test is being run by teamcity:

根据下面的注释,此代码应该能够检查teamcity是否正在运行测试:

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

#2


2  

I'm basically doing that with the following property. It get's the directory name via code base of the calling assembly and if it contains parts of your TeamCity build agent directory it is running within TeamCity.

我基本上是用以下属性做的。它通过调用程序集的代码库获取目录名称,如果它包含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");
    }
}