2

I have to run db scripts before and after test execution but if I mentioned in the Before test run it's running multiple times for every thread but I need to run only once in test execution means it's to be top of all threads

Eg: Hooks file

[BeforeTestRun]
public static void BeforeTestrun()
{
    Console.WriteLine("run before test");
}

[AfterTestRun]
public static void AfterTestrun()
{
    Console.WriteLine("run after test");
}

In Default.srprofile Thread count=3

When run the build its showing like this

  • Thread0: "run before test"
  • Thread1: "run before test"
  • Thread2: "run before test"
  • Thread0: "run after test"
  • Thread1: "run after test"
  • Thread2: "run after test"

But I have to run only once above all threads and after all threads

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
sri
  • 21
  • 2
  • 7
  • 1
    This is a duplicate of https://stackoverflow.com/questions/14775574/is-it-possible-to-run-code-after-all-tests-finish-executing-in-mstest - the accepted answer covers both scenarios – Michael Jones Feb 04 '20 at 17:32
  • Thanks for the response Michael, but how can we use these methods in specflow project? please help me out – sri Feb 05 '20 at 09:40
  • I'm pretty sure you can literally replace `[BeforeTestRun]` with `[AssemblyInitialize]` and `[AfterTestRun]` with `[AssemblyCleanup]`. – Greg Burghardt Feb 05 '20 at 12:22
  • Are you using MSTest? Which unit test provider are you using? That will make a difference. – Greg Burghardt Feb 05 '20 at 12:23
  • I am not using MSTest and I am doing this for automation testing there is no unit test – sri Feb 05 '20 at 12:57
  • I am writing this [BeforeTestrun] and [AfterTestrun] in Hooks file of specflow – sri Feb 05 '20 at 13:01
  • you can use [OneTimeSetUp] for run DB scripts if it needs to run once for the entire code. or in NUnit, there is ITestAction which you can utilize for before test run. Please let me know if more info is required – Krunal Patel Feb 05 '20 at 21:41
  • Thanks krunal, I have to mention this in hooks file of specflow? If I want to run after execution also, is it work?, I am not using NUnit in my framework – sri Feb 06 '20 at 02:05
  • How are you achieving the parallel execution? – Sandesh A D Feb 06 '20 at 08:34
  • By modifying testThreadCount in Default.srprofile – sri Feb 06 '20 at 12:49
  • If you are doing this for automated testing, then you should be using some sort of unit test provider, like MSTest or NUnit. Can you list the NuGet packages installed for your selenium project? – Greg Burghardt Feb 06 '20 at 18:11
  • Yes, I am seeing MSTest.TestFramework in NuGet package, but where can i implement in this, sorry i am new to this – sri Feb 07 '20 at 07:13

1 Answers1

0

If you are using the SpecFlow+Runner with a test thread isolation mode of Process or AppDomain executing the BeforeTestRun and AfterTestRun hooks will happen for every thread.

This is the intended behavior. The reason for this the memory isolation between the threads in these isolation modes.

To have something executed only once for the whole test run you have to use your own deployment step. An example is here: https://github.com/SpecFlowOSS/SpecFlow.Plus.Examples/tree/master/CustomDeploymentSteps

If this happens for other test runners or in the SharedAppDomain, please open an issue on GitHub.


Full disclosure: I am one of the developers of SpecFlow and SpecFlow+.

pablocom
  • 143
  • 1
  • 9
Andreas Willich
  • 5,665
  • 3
  • 15
  • 22
  • Thanks Andrea's but this looks like we have to install WinApp for this , if need to install it restricted to particular system, how can achieve this without this app? – sri Feb 06 '20 at 16:44
  • This is an example how to write a custom deployment step. In it the WinApp driver is used. You can change that part with what you like. It's not needed for the functionality of the custom step – Andreas Willich Feb 06 '20 at 16:47
  • Ohh okay,if we write custom deployment, is it run the Hooks file as same before? because I am doing some other actions in before and after scenarios. – sri Feb 06 '20 at 16:52
  • Can you please give the answer of this also https://stackoverflow.com/q/60044416/5968601 – sri Feb 06 '20 at 16:59