Reputation:
I am trying to add a testcase name as a folder name. I am adding it in Teardown method to detect first whether to testcase passed or fail then accordingly it adds Folder in Pass or Fail Folder which already exist.
[Test]
public void TestCase12345()
{
string Name = methodBase.Name;
teardown(Name);
}
[TearDown]
public void teardown(string testcase)
{
if (TestContext.CurrentContext.Result.Status == TestStatus.Passed)
{
string sourcepath = @"sourcepath";
string timestamp = DateTime.Now.ToString("yy-MM-dd hh-mm");
string destpath = (@"destinationlocation" + "Test Case - " + testcase + " " + timestamp);
...
}
Error:
Invalid signature for SetUp or TearDown method: teardown
What am I missing over here?
Upvotes: 0
Views: 434
Reputation: 13736
Two separate issues here:
1= As others have said, NUnit doesn't allow a teardown method to take an argument.
2 = If you call your method from the test, then it isn't functioning as a TearDown method but as part of the test code. The test has not yet finished, so it has no final result. What's more, if you should have a failed assert, you'll never reach the call.
You should remove the argument and the call. Use TestContext to get the test name as suggested in one of the comments.
Upvotes: 0
Reputation: 50899
You can't pass parameters to [TearDown]
method, NUnit
doesn't support it. For example, to pass parameters to [Test]
you do something like this
[Test]
[TestCase("abc", 123)]
public void TestCase12345(string str, int number)
{
}
But as I said, NUnit
doesn't support it in [TearDown]
.
As a side note, the check if the test succeeded should be in the test method (I find Assert very useful for that purpose). TearDown
should be used only for the "cleaning", i.e. dispose of the WebDriver
and any other things you created for the test and doesn't close automatically when the code is finished.
Edit
"Then what is the solution. how can add function name which I am calling it to create folder?"
You can implement an EventListener interface.
EventListeners are able to respond to events that occur in the course of a test run, usually by recording information of some kind.
For example
public class TestEventListaener : EventListener
{
// The test collection started/finished to run.
void RunStarted(string name, int testCount);
void RunFinished(TestResult result );
void RunFinished(Exception exception );
void TestStarted(TestName testName)
{
// create folder with the test name
}
void TestFinished(TestResult result)
{
// if test succeeded insert data to the folder otherwise delete it
}
// more `EventListener` methods
}
Upvotes: 1
Reputation: 31
In addition to KjetilNodin's answer I would try removing the test case parameter from your tear down function since it is most likely expecting a function without parameters to be used for TearDown. If you need this functionality I'd remove the TearDown attribute from the function and just call it at the end of your test case as in your example.
Upvotes: 0