【发布时间】:2017-10-14 18:31:20
【问题描述】:
我不了解范围界定如何在测试用例中发挥作用。
[TestFixture]
class MockTests
{
[Setup]
private void StartTest()
{
IWebDriver driver = new ChromeDriver();
TestLogger logger = new TestLogger();
}
[TestCase(TestName = "mock1")]
{
//Problem is here. Driver and logger "does not exist in the current context"
driver.Navigate().GoToUrl("http://my.url.com");
logger.Out("Hello! I cannot be accessed!");
}
[TestCase(TestName = "mock2")]
{
//I now have a new instance of driver and logger in my mock2 test
driver.Navigate().GoToUrl("http://my.url.com");
logger.Out("Hello! I am a new Instance of TestLogger!");
}
[TearDown]
private void EndTest()
{
driver.Quit();
logger.PrintReport(TestContext.name, TestContext.status);
}
}
在我看来,如果您不能使用 SetUp 在测试中访问唯一的对象实例,那么 SetUp 是毫无意义的。我看到变量/对象以类似的方式使用,但变量/对象定义在类的范围内;即:私有 IWebDriver 驱动程序;
所以,如果我在一个测试夹具中有 25 个测试并并行运行它们;测试函数可能与错误的驱动程序对话,而不是在每个 TestCase 中创建唯一的实例。
我该如何做才能知道我的 TestLogger 和驱动程序确实是独一无二的?如何在 SetUp 测试和 TearDown 之间传递它们?
TestLogger 只是一个例子。它可以是用户对象模型之类的任何东西:
public class User
{
public string fName{get; set;}
public string lName{get; set;}
//etc...
}
也许我只是不明白应该如何设置测试。
【问题讨论】:
标签: c# visual-studio selenium automated-tests nunit