I have a CodedUI test suite that runs as MSTest Unit tests. I have a method in the AssemblyCleanup that will email the results of the test run that is stored in an external database. How can a programmatically determine how many unit tests were executed during the last run session? I don't want to send the email if we are executing less than 10 tests. This way we only get emails when we run our whole CodedUI suite and not when we are debugging tests. We currently have a SendEmail flag that has to be manually set but I would like this to be programmatically determined.
Asked
Active
Viewed 3,548 times
2
-
1This sounds like a major hack / anti-pattern to me. I wouldn't want to add this kind of tooling in a unit test set. – oɔɯǝɹ Apr 11 '14 at 18:32
-
+1. If a destructive test makes the test run end, results will not get sent. A better idea may be to use vstest.console.exe /logger:trx and parse the trx file for test results. See http://stackoverflow.com/questions/14483837/specifying-results-filename-for-vstest-console-exe if you decide to go this route. – Arun M Apr 13 '14 at 16:42
1 Answers
5
Use a base test class which has a property that keeps the number of the executed tests. All other test classes inherit it and on TestInitialize
increment that property.
Run the following code and see the results on the test output.
[TestClass]
public class BaseTestClass
{
private static int _executedTests;
private static int _passedTests;
[AssemblyCleanup()]
public static void AssemblyCleanup()
{
Console.WriteLine("Total tests executed: {0}", _executedTests );
Console.WriteLine("Total passed tests: {0}", _passedTests);
}
protected void IncrementTests()
{
_executedTests++;
}
protected void IncrementPassedTests()
{
_passedTests++;
}
}
[TestClass]
public class TestClass : BaseTestClass
{
[TestInitialize]
public void TestInitialize()
{
IncrementTests();
}
[TestCleanup]
public void TestCleanup()
{
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Passed)
{
IncrementPassedTests();
}
}
[TestMethod]
public void TestMethod1()
{
}
[TestMethod]
public void TestMethod2()
{
Assert.Fail();
}
public TestContext TestContext { get; set; }
}
Note: The same code can be used on CodedUITests

chaliasos
- 9,659
- 7
- 50
- 87
-
I was just about to implement this but I have mutltiple test classes which means that each time I execute a test in another class a new object is created. Each of these classes are new instance of the TestClass which inherits from the BaseTestClass. This means that each TestClass will have it's own count. I'm checking sending the email in the AssemblyCleanup which will not have access to this information. – PBMax May 15 '14 at 00:54
-
@PBMax, my code will work no matter how many test classes you have. As you can see the counters on the `BaseTestClass` are static so they will be the same for all the different instances of test classes. They will only reset on the next test run. Try it... – chaliasos May 15 '14 at 11:43
-
-
1That worked! I slightly changed the Implementation to increment the counter from the TestInitialize in the base class itself so I didn't have to add the step to all my tests. – PBMax May 16 '14 at 19:31
-
1I then exposed a property to compare the results to determine if I need to send my email. You helped me find a bug in my code too. I didn't quite understand the static concept and that there was only one value across all instances of a class. I thought each class that had it's own value. – PBMax May 16 '14 at 19:59