0

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?

  • I don't know about Selenium, but at least with NUnit you don't call your Attributed methods directly. They're called by NUnit (or Selenium). Since you haven't provided in the attribute a value for `testcase`, Selenium probably doesn't know what to do – Brandon Feb 10 '16 at 15:20
  • I came here for the Selenium btw. Unfortunately I don't have much to contribute with, when the main problem seems to be about C# and nUnit. Good luck :) – jumps4fun Feb 10 '16 at 15:28
  • I think you're misusing the `[TearDown]` attribute. Just remove the attribute and it should all work as you want. – ChrisF Feb 10 '16 at 15:39
  • @ChrisF in which way I am misusing `[TearDown]`. This **`(TestContext.CurrentContext.Result.Status == TestStatus.Passed)`** works in `[TearDown]` only. –  Feb 10 '16 at 15:43
  • @Love0915 - According to the documentation though a `TearDown` method shouldn't have any arguments. This is what's causing your error, but I don't know how to get the `testcase` string into the method. – ChrisF Feb 10 '16 at 15:44
  • Please refer http://stackoverflow.com/questions/35230418/selenium-check-i-the-testcase-pass-or-fail. you will find the answer that it can be used under this. Try it without that if it works for you please add your answer with code. –  Feb 10 '16 at 15:52
  • @Love0915 - see this answer - http://stackoverflow.com/a/29134479/59303 - I think it does what you want to do. – ChrisF Feb 10 '16 at 16:42
  • @Love0915 To put it another way... the message you got was "Invalid signature for SetUp or TearDown method: teardown" Not surprisingly, what it means is that your teardown has an invalid signature!!!! – Charlie Feb 13 '16 at 08:22

3 Answers3

1

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
}
Guy
  • 46,488
  • 10
  • 44
  • 88
  • It does support [TearDown] I have been using it previously only for `PropertiesCollection.driver.Close();` –  Feb 10 '16 at 16:18
  • 2
    @Love0915 `NUnit` support `[TearDown]`, it doesn't support passing parameters to `[TearDown]`. – Guy Feb 10 '16 at 16:19
  • Then what is the solution. how can add function name which I am calling it to create folder –  Feb 10 '16 at 16:21
  • Using an EventListener could work but it's a more complicated solution that what the OP was doing with TearDown. TearDown can work here if it's done right. Several folks, me included, have told him how. – Charlie Feb 11 '16 at 22:44
  • @Charlie I didn't say its the only solution, this is the solution I know and use. – Guy Feb 12 '16 at 06:53
  • @guy Not to put too fine a point on it, I guess I'm saying that the OP can't figure out TearDown, so he has little chance of figuring out addins. :-) – Charlie Feb 13 '16 at 08:15
0

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.

  • I tried it does not work properly without [Teardown]. I hope you know how selenium fixtures works. –  Feb 10 '16 at 15:40
  • 1
    I think you've misunderstood the usage of the TearDown attribute since it expects a parameterless function. Take a look at [link](http://stackoverflow.com/questions/10295054/nunit-passing-parameters-into-teardown-method) specifically the answer regarding TestContext.CurrentContext.Test.Name as I believe this may help you. – Stephen Locke Feb 10 '16 at 15:48
0

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.

Charlie
  • 12,928
  • 1
  • 27
  • 31
  • Refer this Nunit does support TearDown : http://www.nunit.org/index.php?p=teardown&r=2.2.10 –  Feb 10 '16 at 22:13
  • @user3542501 I'm pretty sure nobody suggested NUnit does not support teardown. I sure didn't. The point is that teardown methods don't take arguments. – Charlie Feb 11 '16 at 22:36
  • Also, I hope nobody is following the link to documentation from 2007!!! Use either the latest or the version you are working with yourself. – Charlie Feb 11 '16 at 22:38
  • I edited my answer to clarify that calling the method from your test is not the same as having NUnit call it as a TearDown method. The results won't be the same. – Charlie Feb 11 '16 at 22:49