Skip to main content

[BeforeTestRun] to throw an exception that would fail the test run

Comments

9 comments

  • Andreas Willich

    Which unit test runner are you using and how do you execute it in your pipeline?

    0
  • Victor Sypniewski-Tom

    SpecFlowPlusRunner

    0
  • Andreas Willich

    I assume that you are doing it then with vstest.console.exe or dotnet.exe.

    It could be that VSTest is somehow hiding the exception, because it didn't happened during a scenario.

    Are you executing the scenarios in parallel or not?

    If not, you could move this to a BeforeScenario hook and do only the deploy when you instantiate the device class.

    0
  • Victor Sypniewski-Tom
    Scenarios are sequential.
     
    Exception does show in HTML report. It looks a bit ugly you know something went wrong. The Log file has the exception.

    Moving it to BeforeScenario would be a workaround, but really I need it only once. Maybe a better way would be to run form of ```IsVersionDeployed``` before scenario? Of course it always be correct if it was correct at the first check. 

    I was also thinking about some harsh way of dealing with it, maybe something like:
    System.Environment.Exit(1)

    Is BeforeFeature gonna behave like BeforeTestRun?
     
    Log from CI
    Successfully generated reports.
    Successfully generated reports
    Result: test framework error: At least one test thread aborted.
    Total: 19
    Succeeded: 0
    Ignored: 0
    Pending: 0
    Skipped: 19
    Failed: 0
     
    0
  • Sam Shackleton

    I don't understand what your code is doing in the first post, but it seems you have some sort of precondition for the test run. Is that precondition something which can be verified in a separate script which could be executed before the test run?
    That way you can avoid starting the test run if the precondition isn't met.

     

    If such a script isn't possible, what if you move the logic to a beforescenario hook and cache the result in a way such that the check only occurs once and the result is reused for any subsequent scenarios. 

    0
  • Victor Sypniewski-Tom

    Yeah, no. 

    System.Environment.Exit(1)

    Just gives more confusing error.

    Sam Shackleton Test framework takes care of which version of AUT is being tested. In Pytest-BDD it would have been called test fixture.

    I could write separate console app to do this. Since C# does have API for what I need to do. The throw would not be hidden by VSTest.

    My expectation was that if something throws in the test setup hook, everything would nicely blow up and the run would fail.

    0
  • Victor Sypniewski-Tom

    Sam Shackleton I will consider both options. It would be nice if SpecFlow provided caching mechanism, need to read the manual. In fact it needs to be the first test that is aware if it's looking at the right AUT. 
     

    0
  • Sam Shackleton

    Actually I probably  shouldn't have said caching.

    What about this:

    [Binding]
    public class Hooks
    {
    private static readonly Device _device = new Device();
    private static bool _isVersionDeployed;

    [BeforeTestRun]
    public static async Task Deploy()
    {
    Console.WriteLine("Deploying {0}", _device.Version);
    await _device.Deploy();

    _isVersionDeployed = _device.IsVersionDeployed();
    }

    [BeforeScenario]
    public void EnsureVersion()
    {
    if (!_isVersionDeployed)
    {
    throw new Exception($"Device version: { _device.Version } was not reported as deployed");
    }
    }
    }

    0
  • Victor Sypniewski-Tom

    I went with this. Thanks Sam Shackleton.

    0

Please sign in to leave a comment.

Powered by Zendesk