我已经阅读了一些关于 MStest 的 TestContext 的信息,并且可以相应地使用它。
现在我关于 TestContext 的任务有点不同,我很困惑它是如何工作的。
与三个文件有关的情况:
在 testcase.cs 文件中,TestContext 属性在 [Test Class] 中。但是在[TestMehtod]中,我不想像testContextInstance.WriteLine("WRITE TEST PARAMETERS")这样直接使用,它会放在另一个名为TestLogger.cs的文件中。
[TestClass] public class Test1 : BaseTestTemplate { public TestContext TestContext { get; set; } public static void FixtureSetUp(TestContext testContext) { } public static void FixtureTearDown(TestContext testContext) { } [TestInitialize] public override void SetUp() { } [TestMethod] { Logger.BeginSection("WRITE TEST PARAMETERS"); // instead of testContextInstance.WriteLine("WRITE TEST PARAMETERS"); } }
在 AssemblySetup.cs 文件中,在 [AssemblyInitialize] 中, public static void AssemblySetUp(TestContext testContext) 完成,它包括一个函数 InitializeLogging(); 在这个函数中,我使用 Logger.RegisterLogChannel(LogChannel.TestLog, new TestLogger()) 初始化 TestLogger。
[TestClass] public static class AssemblySetUpClass { public static void InitializeLogging() { string testContext = ""; Logger.RegisterLogChannel(LogChannel.TestLog, new TestLogger(testContext)); } [AssemblyInitialize] public static void AssemblySetUp(TestContext testContext) { InitializeLogging(); } }
下面我在 TestLogger.cs 中添加 testContextInstance.WriteLine(title)。但在调试中,testContextInstance 始终为空。
public sealed class TestLogger : LoggerBase { private TestContext testContextInstance; public TestLogger(string testContext) { } public override void BeginSection(string title) { testContextInstance.WriteLine(title); base.BeginSection(title); } }
我正在尝试修改 Logger.RegisterLogChannel(LogChannel.TestLog, new TestLogger(testContext)) 的目的是告诉TestLogger,会调用testContext。在TestLogger中,我还添加了私有TestContext testContextInstance;和公共TestLogger(字符串testContext)
问题还是一样,在Warning中说,testContextInstance永远不会被赋值,它的默认值永远是null
我希望你能理解我的问题。请给我一些关于如何处理它的想法或解决方案,非常感谢。