代码之家  ›  专栏  ›  技术社区  ›  Naresh

是否仅跳过nunit中特定测试的设置方法?

  •  15
  • Naresh  · 技术社区  · 15 年前

    我有一个不需要运行的测试 SetUp 方法(归因于 [SetUp] )在运行测试之前。我需要 设置 要为其他测试运行的方法。

    是否有不同的属性可以使用或非基于属性的方法来实现这一点?

    6 回复  |  直到 7 年前
        1
  •  15
  •   shsteimer    15 年前

    您应该为该测试创建一个新的类,该类只有它需要的设置(或者缺少设置)。

    或者,您可以将设置代码分解为所有其他测试调用的方法,但我不推荐使用这种方法。

        2
  •  20
  •   chue x    8 年前

    您还可以添加类别并检查设置中的类别列表:

    public const string SKIP_SETUP = "SkipSetup"; 
    
    [SetUp]
    public void Setup(){
       if (!CheckForSkipSetup()){
            // Do Setup stuff
       }
    }
    
    private static bool CheckForSkipSetup() {
        ArrayList categories = TestContext.CurrentContext.Test
           .Properties["_CATEGORIES"] as ArrayList;
    
        bool skipSetup = categories != null && categories.Contains( SKIP_SETUP );
        return skipSetup ;
    }
    
    [Test]
    [Category(SKIP_SETUP)]
    public void SomeTest(){
        // your test code
    }
    
        3
  •  2
  •   DavidRR Chris Brandsma    7 年前

    你可以要主菜 SetUp 在基类中:

    [SetUp]
    public virtual void SetUp()
    {
      // Set up things here
    }
    

    …然后在具有不应运行 设置 代码:

    [SetUp]
    public override void SetUp()
    {
      // By not calling base.SetUp() here, the base SetUp will not run
    }
    
        4
  •  1
  •   DavidRR Chris Brandsma    7 年前

    以下是我建议您完成所需任务的代码:

    public const string SKIP_SETUP = "SkipSetup";
    
    private static bool CheckForSkipSetup()
    {
        string category = string.Empty;
        var categoryKeys = TestContext.CurrentContext.Test.Properties.Keys.ToList();
    
        if (categoryKeys != null && categoryKeys.Any())
            category = TestContext.CurrentContext.Test.Properties.Get(categoryKeys[0].ToString()) as string;
    
        bool skipSetup = (!string.IsNullOrEmpty(category) && category.Equals(SKIP_SETUP)) ? true : false;
    
        return skipSetup;
    }
    
    [SetUp]
    public void Setup()
    {
        // Your setup code
    }
    
    [Test]
    public void WithoutSetupTest()
    {
        // Code without setup
    }
    
    [Test]
    [Category(SKIP_SETUP)]
    public void CodeWithSetupTest()
    {
        // Code that require setup
    }
    
        5
  •  0
  •   AdaTheDev    15 年前

    我不相信你能做到这一点,它需要知道什么测试将要运行,我认为这是不可能的。

    我建议你把它放在一个不同的测试夹具里。

        6
  •  0
  •   DavidRR Chris Brandsma    7 年前

    这是我为实现你想要的目标而建议的代码。

    public const string SKIP_SETUP = "SkipSetup";
    

    现在添加以下方法:

    private static bool CheckForSkipSetup()
    {               
        var categories = TestContext.CurrentContext.Test?.Properties["Category"];
    
        bool skipSetup = categories != null && categories.Contains("SkipSetup");
        return skipSetup;
    }
    

    现在检查条件如下:

    [SetUp]
    public async Task Dosomething()
    {
        if (!CheckForSkipSetup())
        {
    
        }
    }
    

    在测试用例中使用这些方法,如下所示:

    [Test]
    [Category(SKIP_SETUP)]
    public async Task Mywork()
    {
    
    }