I did some tests and used a static field in the class to “tell” the TestCleanup method, which all running methods work. Then you can remove ClassCleanup and do something like this:
private static int runs = 0; [ClassInitialize] public static void SetUpBrowser(TestContext context) { pageObjectBase.SetBrowser("chrome"); pagesManager.GetPageObjectBase(); } [TestMethod] public void FindCriticalBug() { runs++; bla-bla-bla(); } [TestMethod] public void FindCriticalBug2() { runs++; ble-ble-ble(); } [TestCleanup] public static void CloseBrowser() { if (runs == 2) { pageObjectBase.Stop(); pagesManager.GeneralClearing(); } }
I would have kept very far from this solution, but if you have no other alternative and you cannot reorganize your design to use the provided life cycle, this may be an option. You could probably love yourself and write your own base class that takes into account execution and gets the total number of test methods, using reflection to automate this material.
source share