Answered by:
How do I derive from TestContext class and implement from base members

Question
-
So, I see a lot of people are looking at this. But no one is responding to it.
It would be great if someone would be able to at least check the answer to verify it.
----------
Im lazy, and somewhat new to OO.
I am attempting to derive trom the TestContext class. But am not able to use the base class methods when overriding the base class methods...
namespace
TestPlay{public class newTestContext: Microsoft.VisualStudio.TestTools.UnitTesting.TestContext
{
public override void AddResultFile(string fileName){ throw new Exception("The method or operation is not implemented.");
}
public override void BeginTimer(string timerName)
{
throw new Exception("The method or operation is not implemented.");
}
...
I would like to be able to implement the methods via base.AddResultFile ... But doesn't work.
Is that because they are marked as abstract? Wouldn't I be able instiate the testContext class and use that? Thats not working either...
Thanks for your time!!Saturday, December 10, 2005 8:46 AM
Answers
-
With TFS we support publishing of results into a warehouse/database -- take a look at Team Foundation Server. You should be able to find this on MSDN.
If you specifically want to do some custom magic, then maybe store details in the text context, but persist it to your DB using code in a [TestCleanup]
Tuesday, December 20, 2005 9:25 PM -
You would code one method adds data to the test context, (testContext.Properties.Add(blah, blah)), and then write out that data in the [TestCleanup] Method -- there is one test cleanup method per test class, so it can do the same for every one.
The other option is to parse the TRX file post-execution and push that into your custom database.
However, I really would urge you to check out TFS :)
Tuesday, December 20, 2005 11:53 PM
All replies
-
Hi Folks,
I see a lot of people looking at this thread, but no one is posting.
Is there any more information that I need to give?Sunday, December 11, 2005 12:30 AM -
I think I have it figure out now...
//The derived class...
private TestContextHelper testContextInstance;//= new TestContextHelper(); private TestContext testContextInstance1;//used to call the base methods of TestContext
public TestContext TestContext
{
get { return testContextInstance1; }
set { testContextInstance1 = value; }
}//Haven't checked to see if I really need this...
public TestContextHelper TestContextHelper
{
get { return testContextInstance; }
set { testContextInstance = value; }
}[
TestInitialize()]
public void MyTestInitialize()
{
//MAke sure that the derived class is available to all tests...
testContextInstance = new TestContextHelper(TestContext);
}[
TestMethod]
public void TryIT()
{
//Do some testing here...
testContextInstance.WriteLine(
"Did it work?");
}
//Passed in to the overloaded method, writline.
public struct TestResults
{
public int Passed;
public int Failed;
public int TotalVerifications;
public int TotalTests;
public List<string> AssemblyInfo;
public string TestName;
public override string ToString()
{
System.Text.StringBuilder sb = new StringBuilder(25);
sb.AppendLine("Name of test: " + TestName);
sb.AppendLine("Passed: " + this.Passed.ToString());
sb.AppendLine("Failed: " + this.Failed.ToString());
sb.AppendLine("Total Verifications: " + this.TotalVerifications.ToString());
sb.AppendLine("Total Tests: " + this.TotalTests.ToString());
return sb.ToString();
}
} public class TestContextHelper : TestContext
{
private TestContext testContextInstance;
public TestContextHelper(TestContext tc)
{
testContextInstance = tc;
}
public TestContext TC
{
set { testContextInstance = value; }
}
public override void AddResultFile(string fileName)
{
testContextInstance.AddResultFile(fileName);
}
public override void BeginTimer(string timerName)
{
testContextInstance.BeginTimer(timerName);
}
public override System.Data.Common.DbConnection DataConnection
{
get { return testContextInstance.DataConnection; }
} public override DataRow DataRow
{
get { return testContextInstance.DataRow; }
} public override void EndTimer(string timerName)
{
testContextInstance.EndTimer( timerName );
}
public override System.Collections.IDictionary Properties
{
get { return testContextInstance.Properties; }
}
public override void WriteLine(string format, params object[] args)
{
testContextInstance.WriteLine(format, args);
}
public void WriteLine(string format,TestResults tr, params object[] args)
{
testContextInstance.WriteLine(tr.ToString(), args);
tr.TotalTests = tr.Passed + tr.Failed;
Console.WriteLine("Going to the DB now! with tr {0}",tr.ToString());
}
}
So, if anyone is brave enough to try this, or figures out a better way, please let me know!
It looks like MS is using a hidden namespace alltogether to imlement TestContext, that is why I couldn't just call base.[MyfavoriteMethodhere]. The namespace was Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext. or is it a class?
Anyway, confirmation would be greatly appreciated. or an ada boy.Sunday, December 11, 2005 6:06 AM -
What is it you are trying to achive? you shouldnt need to derive from TestContext unless you are trying to write your own test type.Saturday, December 17, 2005 12:04 AM
-
Thanks for your reply.
I would like to be able to store results to a SQL sever DB without having to make to seperate calls. The testContext object seemed to be the most logical class for this. I didn't want to want to have to write my own Console.writeline method because I wanted the functionality of writeline from the testContext class.
Tuesday, December 20, 2005 7:22 PM -
With TFS we support publishing of results into a warehouse/database -- take a look at Team Foundation Server. You should be able to find this on MSDN.
If you specifically want to do some custom magic, then maybe store details in the text context, but persist it to your DB using code in a [TestCleanup]
Tuesday, December 20, 2005 9:25 PM -
So far, the company I work for isn't 100% on team foundation. Besides its not RTM yet is it? They would like to explore as many different ways to solve the problems they have so that the money they have spent on previous solutions wont be a total waist and so that they can decide what they think is the best one. Otherwise I aggree with you.
As far as your solution goes, it doesn't meet my two requirements. I would still have to code more then one call to basicly do the same thing.
From the amount of hits my question has gotten, I would say that there is a HUGE interest in the answer to it. One that is not so specific to the problem I was attempting to solve.
More information on the textContext class and its child objects, would be greatly appreciated.
Again, Thanks for you time!!
Dan
Tuesday, December 20, 2005 10:50 PM -
You would code one method adds data to the test context, (testContext.Properties.Add(blah, blah)), and then write out that data in the [TestCleanup] Method -- there is one test cleanup method per test class, so it can do the same for every one.
The other option is to parse the TRX file post-execution and push that into your custom database.
However, I really would urge you to check out TFS :)
Tuesday, December 20, 2005 11:53 PM