Is it possible to execute code once before all tests run?

前端 未结 3 776
无人共我
无人共我 2020-12-12 21:41

Basically I would like to tell MSTest to execute a bit of code before launching into a series of test runs, essentially what I would like to do is the same thing as sticking

相关标签:
3条回答
  • 2020-12-12 21:54

    I see this in the MS Test header.

    // Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext) { }
    

    This would run before the tests in one class.

    Sounds like you want to run something before all of the tests.

    There is also the setup script option in the test run config.

    0 讨论(0)
  • 2020-12-12 22:12

    FWIW, you can use the AssemblyInitialize attribute to run code before all unit tests in an assembly executes:

    [TestClass]
    public class SetupAssemblyInitializer
    {
        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
            // Initalization code goes here
        }
    }
    

    If you have more than one unit test assembly, I'm not aware of anything that encompasses more than one assembly.

    As far as I'm aware, this is as close as you can get to a Main equivalent.

    Note that the AssemblyInitialize-decorated method must be in a TestClass-decorated class which contains at least one TestMethod-decorated method, otherwise it will not be executed!

    0 讨论(0)
  • 2020-12-12 22:14

    For completion, these are the "run code before" options for MSTest:

    • Use [AssemblyInitialize] to run code once per assembly, before any test in that assembly runs.
    • Use [ClassInitialize] to run code once per class, before any test in the class where the method is defined.
    • Use [TestInitialize] to run code before each and every test in the class where the method is defined.
    0 讨论(0)
提交回复
热议问题