简体   繁体   中英

Private Unit Test Methods?

NOTE: I am NOT asking how to unit test private methods.

What I am asking about is the proper way to have private test methods in a Unit Test (or XUnit in this case) class. Let me show you what I mean:

public class EndToEndTest
{
    private Foo _foo { get; set;}
    private Bar _bar { get; set;}

    [Fact]
    public async Task EndToEnd()
    {
      await TestFoo();
      await TestBar();
      await TestFooBar();
    }

    [Fact]
    private async Task TestFoo()
    {
      _foo = await FooService();
      Assert.NotNull(_foo);
    }

    [Fact]
    private async Task TestBar()
    {
      _bar = await BarService();
      Assert.NotNull(_bar );
    }

    [Fact]
    private async Task TestFooBar()
    {
      var result = FooBarService(_foo, _bar)
      Assert.NotNull(result);
    }
}

The idea here is that the final test is dependent on the first two, and if run alone, it will fail. This works great! But the only exception is that when I look at FooBarService , VisualStudio doesn't seem to fully recognize private async Task TestFooBar() as a valid test... or somethin' like that.

After running the public test EndToEnd , the FooBarService method is showing this:

在此处输入图像描述

This is what I need it to be:

在此处输入图像描述

One of the references is the API and the other is the unit test. So VS is seeing that there is a test method that references the service method, but it doesn't recognize that the method was executed.

What's the right way to do this?

The framework and VS requires test methods to be public for them to be recognized.

Ideally your EndToEnd test should be

[Fact]
public async Task EndToEnd() {
    //Arrange
    Foo _foo = await FooService();
    Assert.NotNull(_foo);

     Bar _bar = await BarService();
     Assert.NotNull(_bar );

    //Act
    var result = FooBarService(_foo, _bar)

    //Assert
    Assert.NotNull(result);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM