如何在xUnit.net测试运行设置code只有一次 [英] How to run setup code only once in an xUnit.net test

查看:445
本文介绍了如何在xUnit.net测试运行设置code只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用设置我的xUnit测试。我有一个要求删除测试文件夹开始的所有图像,然后每个方法做了一些调整图像大小,并保存它的一个副本的输出到文件夹中。该文件夹应该只清空一次,然后每一种方法将节省自己的形象到该文件夹​​。

I'm trying to setup my tests using Xunit. I have a requirement to delete all images in a folder start of the tests, and then each method does some image resizing and saves a copy of it's output to the folder. The folder should only be emptied once, and then each method will save their own image into the folder.

当我使用 IUseFixture< T> ClearVisualTestResultFolder 功能仍然被称为每次测试之前,让我仅在文件夹中的一个图像中结束。

When I use IUseFixture<T>, the ClearVisualTestResultFolder function is still being called before every test, so I only end up with one image in the folder.

public class Fixture
{
    public void Setup()
    {
        ImageHelperTest.ClearVisualTestResultFolder();
    }
}

public class ImageHelperTest : IUseFixture<EngDev.Test.Fixture>
{
    public void SetFixture(EngDev.Test.Fixture data)
    {
        data.Setup();
    }

    public static void ClearVisualTestResultFolder()
    {
        // Logic to clear folder
    }
}

如果我把 ClearVisualTestResultFolder 在构造函数中,它也正在为每个测试方法调用一次。我需要执行的所有测试方法在此之前只运行一次,我怎么能做到这一点?

If I put the ClearVisualTestResultFolder in the constructor, it is also being called once for every test method. I need this just run once before all test methods are executed, how can I achieve this?

如果它的事项,我用ReSharper的测试运行。

If it matters, I use the ReSharper test runner.

推荐答案

在此之后的xUnit讨论主题中的指导,它看起来像你需要实现灯具的构造,也实现IDisposable。下面是表现你想要的方式完整的示例:

Following the guidance in this xUnit discussion topic, it looks like you need to implement a constructor on the Fixture and also implement IDisposable. Here's a complete sample that behaves the way you want:

using System;
using System.Diagnostics;
using Xunit;
using Xunit.Sdk;

namespace xUnitSample
{
    public class SomeFixture : IDisposable
    {
        public SomeFixture()
        {
            Console.WriteLine("SomeFixture ctor: This should only be run once");
        }

        public void SomeMethod()
        {
            Console.WriteLine("SomeFixture::SomeMethod()");
        }

        public void Dispose()
        {
            Console.WriteLine("SomeFixture: Disposing SomeFixture");
        }
    }

    public class TestSample : IUseFixture<SomeFixture>, IDisposable
    {
        public void SetFixture(SomeFixture data)
        {
            Console.WriteLine("TestSample::SetFixture(): Calling SomeMethod");
            data.SomeMethod();
        }

        public TestSample()
        {
            Console.WriteLine("This should be run once before every test " + DateTime.Now.Ticks);
        }

        [Fact]
        public void Test1()
        {
            Console.WriteLine("This is test one.");
        }

        [Fact]
        public void Test2()
        {
            Console.WriteLine("This is test two.");
        }

        public void Dispose()
        {
            Console.WriteLine("Disposing");
        }
    }
}

当从控制台亚军运行此,你会看到下面的输出:

When running this from the console runner, you'll see the following output:

D:\\的xUnit> xunit.console.clr4.exe Test.dll的/ HTML foo.htm xUnit.net
  控制台测试运行器(64位.NET 4.0.30319.17929)版权所有(C)
  2007-11微软公司。

D:\xUnit>xunit.console.clr4.exe test.dll /html foo.htm xUnit.net console test runner (64-bit .NET 4.0.30319.17929) Copyright (C) 2007-11 Microsoft Corporation.

xunit.dll:版本1.9.1.1600测试组装:Test.dll的

xunit.dll: Version 1.9.1.1600 Test assembly: test.dll

SomeFixture构造函数:这应该只运行一次

SomeFixture ctor: This should only be run once

测试完成:2

SomeFixture:处置SomeFixture

SomeFixture: Disposing SomeFixture

2总,0失败,0跳过了0.686秒

2 total, 0 failed, 0 skipped, took 0.686 seconds

然后,当你破解打开测试输出文件foo.htm,你会看到其他的测试输出。

Then, when you crack open the test output file foo.htm, you'll see the other test output.

这篇关于如何在xUnit.net测试运行设置code只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆