最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to access test class state from custom IApplyToTest-attribute? - Stack Overflow

programmeradmin1浏览0评论

I have a test class with TestFixtureSource with two values: "Unit" and "Integration". These values are values of mode. Based on this mode the class sets dependencies up as mocks or real implementations. Some class tests are actual for both modes, while others are actual only for one of them. I am trying to find how to skip or ignore some tests depending on the mode value. So, I want to do something like the following

[TestFixtureSource(nameof(UnitOrIntegration))]
class Tests(string Mode)
{
     [SetUp]
     void Setup()
     {
         if (Mode == "Unit") { ... }
         if (Mode == "Integration") { ... }
     }

     [Test] //actual for both modes
     void Test1() { ... }
     
     [Test][WhenModeIs("Unit")]
     void Test2() { ... }
     
     [Test][WhenModeIs("Integration")]
     void Test3() { ... }
}

I found something that looks like can help

    [AttributeUsage(
          AttributeTargets.Method, 
          AllowMultiple = false,
          Inherited = false)]
    public class WhenModeIsAttribute(string Mode)
         : NUnitAttribute, IApplyToTest
    {
        public void ApplyToTest(Test test)
        {
            if (/* I need check mode here */)
            {
                test.RunState = RunState.Ignored;
                test.Properties.Set(
                   PropertyNames.SkipReason, 
                   $"It is not actual for {Mode}");
            }
        }
    }

But I can't find a way to access the mode value. How to do that?

发布评论

评论列表(0)

  1. 暂无评论