Selenium Visual 的并行测试

我正在尝试使用硒进行并行测试。我正在做的是复制和粘贴测试类,只要我希望我的测试被执行。然后我只需按运行所有测试,它就可以正常工作,但我想知道我是否可以在一个类中多次运行一种方法,而不是复制和发布它。有没有办法做到这一点?


catspeake
浏览 159回答 2
2回答

叮当猫咪

解决方案基本上取决于您使用的测试框架。NUnit 从 3.7 版本开始支持并行测试执行。演示该想法的示例如下using NUnit.Framework;using System;using System.Collections.Generic;using System.Linq;using System.Threading;namespace ConsoleApp1{&nbsp; &nbsp; [TestFixture]&nbsp; &nbsp; public class Dummy&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static TestCaseData Case(int i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => new TestCaseData(TimeSpan.FromSeconds(2)).SetName($"Case {i}");&nbsp; &nbsp; &nbsp; &nbsp; public static IEnumerable<TestCaseData> Cases()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => Enumerable.Range(1, 5).Select(Case);&nbsp; &nbsp; &nbsp; &nbsp; [TestCaseSource(nameof(Cases)), Parallelizable(ParallelScope.Children)]&nbsp; &nbsp; &nbsp; &nbsp; public void ItShouldSleep(TimeSpan t)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => Thread.Sleep(t);&nbsp; &nbsp; &nbsp; &nbsp; static TestCaseData Case2(int i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => new TestCaseData(TimeSpan.FromSeconds(2)).SetName($"Case2 {i}");&nbsp; &nbsp; &nbsp; &nbsp; public static IEnumerable<TestCaseData> Cases2()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => Enumerable.Range(1, 5).Select(Case2);&nbsp; &nbsp; &nbsp; &nbsp; [TestCaseSource(nameof(Cases2)), Parallelizable(ParallelScope.Children)]&nbsp; &nbsp; &nbsp; &nbsp; public void ItShouldSleep2(TimeSpan t)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => Thread.Sleep(t);&nbsp; &nbsp; }&nbsp; &nbsp; [TestFixture()]&nbsp; &nbsp; public class Dummy2&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static TestCaseData Case(int i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => new TestCaseData(TimeSpan.FromSeconds(2)).SetName($"Case {i}");&nbsp; &nbsp; &nbsp; &nbsp; public static IEnumerable<TestCaseData> Cases()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => Enumerable.Range(1, 5).Select(Case);&nbsp; &nbsp; &nbsp; &nbsp; [TestCaseSource(nameof(Cases)), Parallelizable(ParallelScope.Children)]&nbsp; &nbsp; &nbsp; &nbsp; public void ItShouldSleep(TimeSpan t)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => Thread.Sleep(t);&nbsp; &nbsp; }}

www说

我使用您的代码示例对其进行了调整,但问题是它打开了 5 个 Chrome 实例,但它仅适用于一个实例,而不是同时适用于其中的 5 个实例&nbsp; &nbsp;[TestCaseSource(nameof(Cases)), Parallelizable(ParallelScope.Children)]&nbsp; &nbsp; &nbsp; &nbsp; public void ItShouldSleep(TimeSpan t)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChromeOptions options = new ChromeOptions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options.AddArguments("--disable-notifications");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Driver = new ChromeDriver(@"C:\Users\slaouadi\source\repos\UnitTestProject1", options);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string baseUrl = "https://www.google.com/";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Driver.Navigate().GoToUrl(baseUrl);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Driver.FindElement(By.Id("lst-ib")).SendKeys("IT WORKED FINE");&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP