How to run setup code only once in an xUnit.net test

前端 未结 3 1199
我在风中等你
我在风中等你 2020-12-29 22:17

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

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 22:52

    IUseFixture.SetFixture gets called once for each test. The Fixture itself is only created once.

    In other words, you shouldnt be doing anything in your SetFixture method, but you should be instead be triggering it in the Fixture constructor.

    For one-time cleanup, implement an IDisposable.Dispose on the Fixture (it's not required though)

    Note that it's a bad idea to be (even potentially) sharing state between tests. Best to use a TemporaryDirectoryFixture like this one.

提交回复
热议问题