Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 works yay... so I Move on...

Now I have written another test and it works as I have saved and tested it like above. But, they dont work when they are run together.

Here are my two tests that work when run as individuals but fail when run together:

using System; using NUnit.Framework; using OpenQA.Selenium.Support.UI; using OpenQA.Selenium;  namespace Fixtures.Users.Page1 {     [TestFixture]     public class AdminNavigateToPage1 : SeleniumTestBase     {         [Test]         public void AdminNavigateToPage1()         {             NavigateTo<LogonPage>().LogonAsCustomerAdministrator();             NavigateTo<Page1>();             var headerelement = Driver.FindElement(By.ClassName("header"));              Assert.That(headerelement.Text, Is.EqualTo("Page Title"));             Assert.That(Driver.Url, Is.EqualTo("http://localhost/Page Title"));         }          [Test]         public void AdminNavigateToPage1ViaMenu()         {             NavigateTo<LogonPage>().LogonAsCustomerAdministrator();             Driver.FindElement(By.Id("menuitem1")).Click();             Driver.FindElement(By.Id("submenuitem4")).Click();             var headerelement = Driver.FindElement(By.ClassName("header"));              Assert.That(headerelement.Text, Is.EqualTo("Page Title"));             Assert.That(Driver.Url, Is.EqualTo("http://localhost/Page Title"));         }     } } 

When the second test fails because they have been run together

Nunit presents this:

Sse.Bec.Web.Tests.Fixtures.ManageSitesAndUsers.ChangeOfPremises.AdminNavigateToChangeOfPremises.AdminNavigateToPageChangeOfPremisesViaMenu: OpenQA.Selenium.NoSuchElementException : The element could not be found

And this line is highlighted:

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

Does anyone know why my code fails when run together, but passes when run alone?

Any answer would be greatly appreciated!

like image 435
Graeme Secondwave Avatar asked Jan 24 '12 13:01

Graeme Secondwave


People also ask

Do jest tests run sequentially?

Order of Execution​ Once the describe blocks are complete, by default Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.

Should tests depend on each other?

Tests should never depend on each other. If your tests have to be run in a specific order, then you need to change your tests. Instead, you should make proper use of the Setup and TearDown features of your unit-testing framework to ensure each test is ready to run individually.


1 Answers

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.
like image 88
user3613932 Avatar answered Sep 19 '22 15:09

user3613932