Why do my tests fail when run together, but pass individually?

前端 未结 7 1131
有刺的猬
有刺的猬 2020-12-29 23:56

When I write a test in Visual Studio, I check that it works by saving, building and then running the test it in Nunit (right click on the test then run).

The test wo

相关标签:
7条回答
  • 2020-12-30 00:23

    look into the TestFixtureSetup, Setup, TestFixtureTearDown and TearDown.
    These attributes allow you to setup the testenvironment once, instead of once per test.

    0 讨论(0)
  • 2020-12-30 00:23

    Without knowing how Selenium works, my bet is on Driver which seems to be a static class so the 2 tests are sharing state. One example of shared state is Driver.Url. Because the tests are run in parallel, there is a race condition to set the state of this object.

    That said, I do not have a solution for you :)

    0 讨论(0)
  • 2020-12-30 00:24

    Two things you can try

    1. put the break point between the following two lines. And see which page are you in when the second line is hit
    2. Introduce a slight delay between these two lines via Thread.Sleep

      Driver.FindElement(By.Id("submenuitem4")).Click(); var headerelement = Driver.FindElement(By.ClassName("header"));

    0 讨论(0)
  • 2020-12-30 00:24

    If none of the answers above worked for you, i solved this issue by adding Thread.Sleep(1) before the assertion in the failing test...

    Looks like tests synchronization is missed somewhere... Please note that my tests were not order dependant, that i haven't any static member nor external dependency.

    0 讨论(0)
  • 2020-12-30 00:25

    Such a situation normally occurs when the unit tests are using shared resources/data in some way.

    1. It can also happen if your system under test has static fields/properties which are being leveraged to compute the output on which you are asserting.
    2. It can happen if the system under test is being shared (static) dependencies.
    0 讨论(0)
  • 2020-12-30 00:29

    Are you sure that after running one of the tests the method

    NavigateTo<LogonPage>().LogonAsCustomerAdministrator();
    

    is taking you back to where you should be? It'd seem that the failure is due to improper navigation handler (supposing that the header element is present and found in both tests).

    0 讨论(0)
提交回复
热议问题