简体   繁体   English

为什么我的测试一起运行时失败,但单独通过?

[英]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).当我在 Visual Studio 中编写测试时,我通过保存、构建然后在 Nunit 中运行测试来检查它是否有效(右键单击测试然后运行)。

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: Nunit 介绍了这一点:

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

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!任何答案将不胜感激!

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.如果被测系统正在共享(静态)依赖项,则可能会发生这种情况。

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...如果以上答案都不适合您,我通过在失败测试中的断言之前添加Thread.Sleep(1)解决了这个问题......

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.看起来测试同步在某处丢失了......请注意,我的测试不依赖于顺序,我没有任何静态成员或外部依赖。

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通过 Thread.Sleep 在这两行之间引入一点延迟

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

Without knowing how Selenium works, my bet is on Driver which seems to be a static class so the 2 tests are sharing state.在不知道 Selenium 是如何工作的情况下,我的赌注是Driver ,它似乎是一个静态类,所以 2 个测试共享状态。 One example of shared state is Driver.Url .共享状态的一个示例是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 :)也就是说,我没有适合你的解决方案:)

look into the TestFixtureSetup , Setup , TestFixtureTearDown and TearDown .查看TestFixtureSetupSetupTestFixtureTearDownTearDown
These attributes allow you to setup the testenvironment once, instead of once per test.这些属性允许您设置一次测试环境,而不是每次测试一次。

I think you need to ensure, that you can log on for the second test, this might fail, because you are logged on already?我认为您需要确保您可以登录进行第二次测试,这可能会失败,因为您已经登录了?

-> putting the logon in a set up method or (because it seems you are using the same user for both tests) even up to the fixture setup -> the logoff (if needed) might be put in the tear down method -> 将登录放在设置方法中,或者(因为看起来你在两个测试中使用相同的用户)甚至到夹具设置-> 注销(如果需要)可能会放在拆卸方法中

     [SetUp]
     public void LaunchTest()
     {
        NavigateTo<LogonPage>().LogonAsCustomerAdministrator();
     }

     [TearDown]
     public void StopTest()
     {
        // logoff
     }
     [Test]
     public void Test1()
     {...}
     [Test]
     public void Test2()
     {...}

If there are delays in the DOM instead of a thread.sleep I recommend to use webdriver.wait in combination with conditions.如果 DOM 有延迟而不是 thread.sleep,我建议结合条件使用 webdriver.wait。 The sleep might work in 80% and in others not.睡眠可能在 80% 中起作用,而在其他情况下则不起作用。 The wait polls until a timeout is reached which is more reliable and also readable.等待轮询直到达到更可靠且可读的超时。 Here an example how I usually approach this:这是一个我通常如何处理的示例:

    var webDriverWait = new WebDriverWait(webDriver, ..);
    webDriverWait.Until(d => d.FindElement(By.CssSelector(".."))
        .Displayed))

I realize this is an extremely old question but I just ran into it today and none of the answers addressed my particular case.我意识到这是一个非常古老的问题,但我今天才遇到它,没有一个答案能解决我的特殊情况。

Using Selenium with NUnit for front end automation tests.使用 Selenium 和 NUnit 进行前端自动化测试。

For my case I was using in my startup [OneTimeSetUp] and [OneTimeTearDown] trying to be more efficient.对于我的情况,我在启动时使用[OneTimeSetUp][OneTimeTearDown]试图提高效率。

This however has the problem of using shared resources, in my case the driver itself and the helper I use to validate/get elements.然而,这存在使用共享资源的问题,在我的情况下是驱动程序本身和我用来验证/获取元素的助手。

Maybe a strange edge case - but took me a few hours to figure it out.也许是一个奇怪的边缘案例——但我花了几个小时才弄清楚。

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).失败似乎是由于导航处理程序不当造成的(假设在两个测试中都存在并找到了 header 元素)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试在一起运行时失败,单独传递 - Unit Tests fail when run together, pass individually 为什么使用HostType(“ Moles”)进行的单元测试中的断言在单独运行时可以通过,而在一组测试中运行时却失败? - Why would an assert on a unit test with HostType(“Moles”) pass when run individually, but fail when run with a group of tests? Flurl&HttpTest:单元测试在全部运行时失败,但在单独运行时通过 - Flurl & HttpTest: Unit tests fail when Run All, but pass when run individually 一起运行时单元测试超时,单独运行时成功吗? - Unit Tests timeout when run together, succeed when run individually? 单元测试在Visual Studio中“全部运行”但单独传递时失败 - Unit Tests Fail when “Run All” in Visual Studio but passes individually 单元测试在“全部运行”时失败,但在单次运行时通过 - Unit tests fail on Run All but pass when are run single 一些 Nunit 测试在组中失败,但在其他环境中单独运行时通过 - Some Nunit tests are failing in groups but pass when run individually in other env 我正在尝试在Visual Studio中运行所有测试,但是当我运行测试时,第一个测试将通过,但所有其他测试将失败 - I am trying to run all tests in visual studio, but when I run the the tests the first one will pass, but all the others will fail 如果使用try / catch,为什么我的C#单元测试的异常失败? - Why do my C# unit tests for exceptions fail if I use try/catch? C#Selenium WebDriver测试在连续运行时失败但在单独运行时成功 - C# Selenium WebDriver tests failing when run consecutively but succeeding when run individually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM