.NET Core:NUnit不会执行SetUp

时间:2017-04-16 21:15:19

标签: c# .net unit-testing asp.net-core

除非我手动调用,否则NUnit不会执行SetUp方法。另外,当我将SetUp()添加到我的测试中时,我似乎也遇到了测试服务器的问题,因为我得到了" StatusCode:204,ReasonPhrase:' No Content'"在响应中,但如果我执行它,控制器工作正常。我使用的是.NET Core 1.1和NUnit 3.6.1。

解决方案在https://github.com/Narcil/NUnitMCV

我的测试班:

[TestFixture]
public class FrontendControllerTests : AbstractFrontendTestFixtures
{
    [Test]
    public async Task FrontendAppSettingsTest()
    {
        var response = await _client.GetAsync("/api/values");
        var value = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());

        Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        Assert.IsTrue(value.Equals("Some FrontendValue"));
    }

}

我的抽象类

public abstract class AbstractFrontendTestFixtures
{
    protected HttpClient _client;
    protected TestServer _server;

    [SetUp]
    protected void SetUp()
    {
        _server = new TestServer(new WebHostBuilder()
            .UseStartup<Frontend.Startup>()
            .UseEnvironment("Testing"));
        _client = _server.CreateClient();
    }

    [TearDown]
    protected void TearDown()
    {
        _client.Dispose();
        _server.Dispose();
    }
}

1 个答案:

答案 0 :(得分:1)

您的SetUp方法应为public。如果您希望在派生类中有其他设置逻辑,只需将方法标记为virtual