簡體   English   中英

如何在Teamcity上使用ApprovalTests?

[英]How to use ApprovalTests on Teamcity?

我正在使用審批測試 在我的開發機器上,當我的測試結果與批准的不DiffReporter ,我很滿意啟動TortoiseDiff的 DiffReporter

    [UseReporter(typeof (DiffReporter))]
    public class MyApprovalTests
    { ... }

但是,當在Teamcity上運行相同的測試並且結果不同時,測試將失敗,並顯示以下錯誤:

System.Exception : Unable to launch: tortoisemerge.exe with arguments ...
Error Message: The system cannot find the file specified
---- System.ComponentModel.Win32Exception : The system cannot find the file 
                                                                 specified

顯然它找不到tortoisemerge.exe,這很好,因為它沒有安裝在構建代理上。 但是如果它被安裝了怎么辦? 然后對於每個失敗,另一個tortoisemerge.exe實例將啟動,沒有人會關閉它。 最終噸的tortoisemerge.exe實例將殺死我們的服務器:)

所以問題是 - 如何修改測試以在本地機器上運行Tortoise Diff並僅在構建服務器上報告錯誤? 我知道#IF DEBUG [UseReporter(typeof (DiffReporter))]但是如果可能的話會更喜歡另一種解決方案。

記者和CI的問題有幾個解決方案。 我將列出所有這些,然后指向一個更好的解決方案,這還沒有完全啟用。

  1. 使用AppConfigReporter。 這允許您在AppConfig中設置報告者,並且可以使用QuietReporter for CI。 這里有一個視頻,還有很多其他的記者。 AppConfigReporter出現在6:00。 這具有單獨配置的優點,您可以在程序集級別進行裝飾,但缺點是如果在類/方法級別覆蓋,則仍然存在問題。

  2. 創建自己的(2)記者。 值得注意的是,如果您使用記者,它將被調用,無論它是否在環境中工作。 IEnvironmentAwareReporter允許復合記者,但不會阻止直接調用記者。 很可能你需要2個記者,一個什么都不做(比如一個安靜的記者)但只能在你的CI服務器上工作,或者在TeamCity調用時。 將其稱為TeamCity Reporter。 而One,這是一個multiReporter,如果它工作正在調用teamCity,否則推遲。

  3. 使用FrontLoadedReporter(還沒准備好)。 這就是ApprovalTests目前使用NCrunch的方式。 它在UseReporter屬性中加載的任何內容之前執行上述方法。 我一直想添加一個程序集級屬性來配置它,但還沒有(抱歉)我會盡快添加它。

希望這可以幫助。 盧埃林

我最近自己遇到了這個問題。

借用xunit以及他們如何處理TeamCity日志,我想出了一個基於NCrunch Reporter的TeamCity Reporter。

public class TeamCityReporter : IEnvironmentAwareReporter, IApprovalFailureReporter
{
    public static readonly TeamCityReporter INSTANCE = new TeamCityReporter();

    public void Report(string approved, string received) { }

    public bool IsWorkingInThisEnvironment(string forFile)
    {
        return Environment.GetEnvironmentVariable("TEAMCITY_PROJECT_NAME") != null;
    }
}

所以我可以將它與NCrunch記者結合起來:

public class TeamCityOrNCrunchReporter : FirstWorkingReporter
{
    public static readonly TeamCityOrNCrunchReporter INSTANCE = 
        new TeamCityOrNCrunchReporter();

    public TeamCityOrNCrunchReporter()
        : base(NCrunchReporter.INSTANCE,
        TeamCityReporter.INSTANCE) { }
}

[assembly: FrontLoadedReporter(typeof(TeamCityOrNCrunchReporter))]

我想出了一個小想法。

您可以實現自己的記者,讓我們稱之為DebugReporter

public class DebugReporter<T> : IEnvironmentAwareReporter where T : IApprovalFailureReporter, new()
{
    private readonly T _reporter;

    public static readonly DebugReporter<T> INSTANCE = new DebugReporter<T>();

    public DebugReporter()
    {
        _reporter = new T();
    }

    public void Report(string approved, string received)
    {
        if (IsWorkingInThisEnvironment())
        {
            _reporter.Report(approved, received);
        }
    }

    public bool IsWorkingInThisEnvironment()
    {
#if DEBUG
        return true;
#else
        return false;
#endif
    }
}

使用示例,

[UseReporter(typeof(DebugReporter<FileLauncherReporter>))]
public class SomeTests
{
    [Test]
    public void test()
    {
        Approvals.Verify("Hello");
    }
}

如果測試是faling,它仍然是紅色 - 但記者不會出現。

IEnvironmentAwareReporter是專門為此定義的,但不幸的是,無論我返回什么,它仍然調用Report()方法。 所以,我把IsWorkingInThisEnvironment()調用放在里面,這有點hackish,但工作:)

希望Llywelyn可以解釋為什么它會像那樣。 (錯誤?)

我正在使用CC.NET,我在服務器上安裝了TortoiseSVN。

我重新配置了構建服務器以允許CC.NET服務與桌面交互。 當我這樣做時,TortiseMerge推出了。 所以我認為正在發生的事情是Approvals嘗試啟動該工具,但它不能因為CC.NET作為服務運行而操作系統默認會阻止該行為。 如果TeamCity作為服務運行,您應該沒問題,但您可能想要測試。

暫無
暫無

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

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