What's the difference between using the constructor in VS Testing framework vs. TestInitialize() attribute?

后端 未结 3 1822
庸人自扰
庸人自扰 2021-02-07 00:47

Quick question, I\'m using the Visual Studio\'s testing framework for unit testing. Just wondering what\'s the difference between using the constructor to do initialization work

3条回答
  •  半阙折子戏
    2021-02-07 00:59

    Conceptually they are they same, as MSTest creates a new instance of your test class before each test execution. However, technically there are a few differences:

    1. The ctor is called before TestInitialize (no surprise as the latter is an instance method).
    2. You have access to TestContext in TestInitialize.
    3. More inheritance scenarios are enabled with TestInitialize: https://stackoverflow.com/a/8689398/67824.
    4. You can assign readonly fields in the ctor. I think it's pretty important: https://stackoverflow.com/a/45270180/67824.

提交回复
热议问题