Ignore scenarios based on environment

1,205 views
Skip to first unread message

Soujanya R

unread,
Mar 28, 2017, 2:21:53 PM3/28/17
to spec...@googlegroups.com
Hi,

Any help is appreciated.

I am trying to ignore some scenarios based on environment selection in BeforeScenario block. I am using Specflow+Webdriver+C#+Visual studio MStest.

I tried below
  [BeforeScenario]
   public void BeforeScenarion()
   {
       if ((ScenarioContext.Current.ScenarioInfo.Tags.Contains("SupportedOnlyInIntegration") ||
            FeatureContext.Current.FeatureInfo.Tags.Contains("SupportedOnlyInIntegration")) &&
           WebDriver.environment == "Staging")
       {
           //ScenarioContext.Current.ScenarioInfo.Tags.ToList().Add("ignored");
           try
           {
               var unitTestRuntimeProvider = (IUnitTestRuntimeProvider)
                   ScenarioContext.Current
                       .GetBindingInstance((typeof(IUnitTestRuntimeProvider)));
               unitTestRuntimeProvider.TestIgnore("ignored");
           }
           catch(Exception e)
           {
    
           }
                    ......
                    .......
                  .........
                  ........
   }

I am getting Assert.inconclusive failed.ignored exception. Is there any other way to ignore test scenarios based on environment and tag?

Thanks!

Yurij

unread,
Mar 29, 2017, 3:50:43 AM3/29/17
to SpecFlow
If I right understand:
            
if (ScenarioContext.Current.ScenarioInfo.Tags.Contains("SupportedOnlyInIntegration") 
    || FeatureContext.Current.FeatureInfo.Tags.Contains("SupportedOnlyInIntegration")) 
    && WebDriver.environment == "Staging")
 {
        SpecRunner.TestIgnored($"Test ignored because environment: {WebDriver.environment}");
 }


вторник, 28 марта 2017 г., 21:21:53 UTC+3 пользователь Soujanya R написал:

rsou...@gmail.com

unread,
Mar 29, 2017, 11:04:12 AM3/29/17
to SpecFlow
Hi Yurij,

Thanks for your response! Is there a way without using SpecRunner?

Andreas Willich

unread,
Mar 29, 2017, 11:11:17 AM3/29/17
to SpecFlow
At the end a SpecFlow scenario is a test, which is executed in your case by MsTest. If it is possible in MsTest, you probably can do it also in a SpecFlow step binding.

Perhaps it helps you.

BR
Andreas

--
You received this message because you are subscribed to the Google Groups "SpecFlow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specflow+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rsou...@gmail.com

unread,
Mar 29, 2017, 11:23:29 AM3/29/17
to SpecFlow
Thanks Andreas! I am trying to ignore test only for one environment but I want to run the same test for other environment.Below link is to skip the test completely.

Yurij

unread,
Mar 29, 2017, 3:09:34 PM3/29/17
to SpecFlow
You can try this approach in AfterScenario:

        if (ScenarioContext.Current.TestError != null
           
&& ScenarioContext.Current.TestError is IgnoredException)
       
{
           
var exception = ScenarioContext.Current.TestError;
           
PropertyInfo testStatusProperty = typeof(ScenarioContext).GetProperty("TestStatus", BindingFlags.Instance | BindingFlags.NonPublic);
           
PropertyInfo testErrorProperty = typeof(ScenarioContext).GetProperty("TestError", BindingFlags.Instance | BindingFlags.NonPublic);

           
var testStatus = testStatusProperty.GetValue(ScenarioContext.Current, null); // for debug
           
var testError = testErrorProperty.GetValue(ScenarioContext.Current, null);    // for debug

            testStatusProperty
.SetValue(ScenarioContext.Current, TestStatus.OK, null);
            testErrorProperty
.SetValue(ScenarioContext.Current, null, null);
       
}

Yurij

unread,
Mar 29, 2017, 3:24:13 PM3/29/17
to SpecFlow
Name "IgnoredException" for example

rsou...@gmail.com

unread,
Mar 30, 2017, 11:50:35 AM3/30/17
to SpecFlow
Thank you much Yurij! I tried as you said but no luck yet.

 if (ScenarioContext.Current.TestError != null && (ScenarioContext.Current.ScenarioInfo.Tags.Contains("SupportedOnlyInIntegration") ||
                 FeatureContext.Current.FeatureInfo.Tags.Contains("SupportedOnlyInIntegration")) &&
                WebDriver.environment == "Integration")

            {
                var exception = ScenarioContext.Current.TestError;
                PropertyInfo testStatusProperty = typeof(ScenarioContext).GetProperty("TestStatus", BindingFlags.Instance | BindingFlags.NonPublic);
                var testErrorProperty = typeof(ScenarioContext).GetProperty("TestError", BindingFlags.Instance | BindingFlags.Public);

               
                var testError = testErrorProperty.GetValue(ScenarioContext.Current, null);
                var testStatus = testStatusProperty.GetValue(ScenarioContext.Current, null); // for debug
               

                testStatusProperty.SetValue(ScenarioContext.Current, TestStatus.OK, null);
                testErrorProperty.SetValue(ScenarioContext.Current, null, null);
                WebDriver.driver.Manage().Cookies.DeleteAllCookies();
                return;

            }
Though testErrorproperty is set to null and status as ok...once after Afterscenario block is done i am still getting the test as failed with exception message.

Test Name:   ....._ApiUsers65
Test Outcome:    Failed
Result Message:   
Test method ......._ApiUsers65 threw exception:
System.UriFormatException: Invalid URI: The format of the URI could not be determined.
Result StandardOutput:   
Given Rest client api/users/65
-> error: Invalid URI: The format of the URI could not be determined.
And Request Type "GET"
-> skipped because of previous errors
When "GET" Method is executed
-> skipped because of previous errors
Then Sucessful Response is generated
-> skipped because of previous errors
And Json response is parsed
-> skipped because of previous errors
And Parsed Json is compared with db User details by ID 65 is verified
-> skipped because of previous errors



On Wednesday, March 29, 2017 at 3:24:13 PM UTC-4, Yurij wrote:
Name "IgnoredException" for example

rsou...@gmail.com

unread,
Mar 31, 2017, 5:05:58 PM3/31/17
to SpecFlow
Just to update on the fix-
finally old code worked fine just by not placing it in try catch blocks
In BeforeScenario --
if ((ScenarioContext.Current.ScenarioInfo.Tags.Contains("SupportedOnlyInIntegration") ||
                 FeatureContext.Current.FeatureInfo.Tags.Contains("SupportedOnlyInIntegration")) &&
                WebDriver.environment == "Staging")

            {
                var unitTestRuntimeProvider = (IUnitTestRuntimeProvider)
                    ScenarioContext.Current
                        .GetBindingInstance((typeof(IUnitTestRuntimeProvider)));
                unitTestRuntimeProvider.TestIgnore("ignored");           
            }

Output is like below-

Test Outcome: Skipped

Test Duration:       0:00:00.0263362

 

Result Message:      Assert.Inconclusive failed. ignored


Thank you!!
Reply all
Reply to author
Forward
0 new messages