NUnit: How to pass TestCaseData from a non-static method?

后端 未结 2 1502
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 17:00

my test fails because of : Message: The sourceName specified on a TestCaseSourceAttribute must refer to a static field, property or method.

This is my Code:



        
相关标签:
2条回答
  • 2021-02-13 17:04

    By design, the method, property or field used by the TestCaseSourceAttribute must be static. This is intended to avoid the need to instantiate the fixture class at the time the tests are loaded. Your fixture is only instantiated when we start the run - in the case of the GUI, each time we start the run - and its lifetime is only as long as it takes to run the fixture.

    In your case, you have appear to have discovered that you can use a static method. That's best, if possible.

    The only way to use instance methods here is to use the constructor TestCaseSourceAttribute(Type sourceType) where sourceType implements IEnumerable and returns your test case data directly. If you use this, I recommend using a different class from your TestFixture. It's not absolutely necessary. If you use the same class, different instances will be created at load time and run time, which have no connection whatsoever with one another. Many developers end up getting confused by this and try to leave state behind at load time for use by the tests. That won't work.

    0 讨论(0)
  • 2021-02-13 17:09

    0 讨论(0)
提交回复
热议问题