Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, January 3, 2011 5:37 AM | 1 vote
I am not able to get the testdata of the mthod which i have called in another method.
For example testMethod1(contains testdata) and testmethod2(contains testdata)
I want call testmethod1 with the testdata in testmethod2(which also contains testdata)
testmethod2()
{
testmethod1(need testdata also to be called)
}
Can some one help on this?
Thanks in Advance,
Satya chenna.
All replies (5)
Wednesday, January 5, 2011 7:00 AM ✅Answered
Hi chennasatya,
Welcome to the MSDN forum. Hope you can learn as well as contributing to our community.
If I have not misunderstood you, your requirement is:
There is an inner method which is called within an outer method, suppose there is an object or variable named testdata in the outer method, you want the change made to the testdata in the inner method to also take effect in the outer method.
If so, the solution is to pass the testdata as a parameter into the inner method, use out/ref keyword to modify the parameter.
Please check detailed information about Pass By Value and Pass By Reference with the knowledge bases given as below.
**Passing Parameters
**http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx
**C# Parameter: Pass object by value, The copy constructor
**http://www.codeproject.com/KB/cs/parameter_object_by_value.aspx
**Passing Reference-Type Parameters (C# Programming Guide)
**http://msdn.microsoft.com/en-us/library/s6938f28.aspx
I hope the information supplied meets your requirement. If any further concerns exist please contact me. Thanks.
Leo Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Wednesday, January 5, 2011 8:01 AM ✅Answered | 1 vote
I suspect the OP is talking about Unittests. In this case, you cannot choose the signature of your testmethods, they need to be void and have no paramaters (unsure about the former, sure about the latter).
My first attempt at an answer would be: store the TestData in a property on your TestClass.
[TestClass]
public class SomeDataTest{
private string data;
[TestMethod]
public void TestSomething(){
Assert.IsNotNull("hello");
this.data = "hello";
}
[TestMethod]
public void TestSomethingElse(){
Assert.IsNotNull(this.data);
}
}
However, I see two problems with this approach: the first being that you cannot change a test independant of another test. This kind of beats the purpose of the tests, they should probably be just one test then. The second being that you assume that TestMethod1 will always be called BEFORE TestMethod2, but as to my knowledge, there's no guarantee from any software testing tool (like the one built in Visual Studio), that the order in which tests are executed are fixed between runs, versions, ...
A better approach would be to split the test, and add a method (not a testmethod) where your testdata is built:
[TestClass]
public class SomeDataTest{
private string data;
[TestMethod]
public void TestSomething(){
Assert.IsNotNull(GetTestData());
}
[TestMethod]
public void TestSomethingElse(){
AssertIsTrue(GetTestData().Length > 5);
}
private static string GetTestData(){
return "Hello" + "World";
}
}
The best approach, if your testdata is large, is to keep your data in seperate files.
[TestClass]
public class SomeDataTest{
private string data;
[TestMethod]
public void TestSomething(){
Assert.IsNotNull(GetTestData("SomeDataTest.TestSomething.xml"));
}
[TestMethod]
public void TestSomethingElse(){
AssertIsTrue(GetTestData("SomeDataTest.TestSomethingElse.xml").Length > 5);
}
private static string GetTestData(string resourcePath){
//Read the xml
//Parse the data you need
//Return the data as an object.
}
}
In case these replies were not helpful, or not helpful enough, feel free to post some more code so people can get a good idea of where you're stuck.
Happy coding!
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Wednesday, January 5, 2011 12:36 PM ✅Answered
Hi,
If the OP does mean Unit Testing then it's a bad idea having one test call another test.
Creates a dependency between the tests which should always be avoided.
If there is some initial set up code in one test that needs to be called from the other test then extract that setup code from the test method and put it in it's own method and call this new method from both tests.
Some real code would be good.
…we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
Monday, January 3, 2011 5:57 AM
Hi,
//With out parameter
private string TestMethod1()
{
return "Hello World";
}
private void TestMethod2()
{
string strMsg = TestMethod1();
MessageBox.Show(strMsg);
}
//with parameter
private string TestMethod1(string strMsg)
{
return strMsg;
}
private void TestMethod2()
{
string strMsg = TestMethod1("Hello world");
MessageBox.Show(strMsg);
}
Nagarjuna Dilip
Monday, January 10, 2011 3:14 AM
Hi chennasatya,
As you have not mentioned about your usage, now I will mark useful answers to your issue.
If any one does not do good to your issue please “Unmark As Answer”.
Thanks for you understanding.
If any further concerns exist please contact me. Thanks.
Sincerely,
Leo LiuLeo Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.