【发布时间】:2021-12-22 14:19:54
【问题描述】:
我正在使用 NUnits IEnumerable 尝试在 Nunit SetUpFixture 测试基类中按方法名称返回测试数据(见类底部):
[SetUpFixture]
public class TestBase
{
private readonly BrowserFactory _factory = new BrowserFactory();
protected IWebDriver Driver;
private static ExtentReports _extent;
private static string _projectPath;
protected ExtentTest _test;
private static TestContext testContext;
[OneTimeSetUp]
public void OneTimeSetUp()
{
InitializeReport();
_test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
Driver = _factory.Init(Config.Browser);
Driver.Manage().Window.Maximize();
Driver.Manage().Cookies.DeleteAllCookies();
}
[TearDown]
public void TearDown()
{
var status = TestContext.CurrentContext.Result.Outcome.Status;
string testClassName = TestContext.CurrentContext.Test.Name;
string testMethodName = TestContext.CurrentContext.Test.MethodName;
var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace) ? "" : string.Format("{0}", TestContext.CurrentContext.Result.StackTrace);
Status logstatus;
switch (status)
{
case TestStatus.Failed:
logstatus = Status.Fail;
Console.WriteLine("Test status is Fail");
DateTime time = DateTime.Now;
String fileName = "Screenshot_" + time.ToString("h_mm_ss") + ".png";
String screenShotPath = Capture(Driver, fileName);
_test.Log(Status.Fail, "Fail");
_test.Log(Status.Fail, "Snapshot below: " + _test.AddScreenCaptureFromPath("Screenshots\\" + fileName));
break;
case TestStatus.Inconclusive:
logstatus = Status.Warning;
break;
case TestStatus.Skipped:
logstatus = Status.Skip;
break;
default:
logstatus = Status.Pass;
Console.WriteLine("Test status is Pass");
break;
}
_test.Log(logstatus, "Test " + " - " + testMethodName + " Ended with a status of " + logstatus + stacktrace);
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
CloseBrowser();
_extent.Flush();
}
public void CloseBrowser()
{
Driver?.Close();
Driver?.Quit();
Driver?.Dispose();
}
public static IEnumerable<JObject> GetTestData()
{
string testMethodName = TestContext.CurrentContext.Test.MethodName;
JObject[] testDataReturned = GetDataByMethod(testMethodName);
for (int i = 0; i < testDataReturned.Length; i++)
{
yield return testDataReturned[i];
}
}
public static JObject[] GetDataByMethod(string testMethodName)
{
return JsonUtils.FetchData(testMethodName);
}
}
}
我的问题是,当我运行此代码时,IEnumerable 方法GetTestData() 中的字符串testMethodName 的值为AdhocTestMethod,而不是实际的测试方法字符串LoginTest_E2E。
我可以在 OneTimeSetUp() 和 TearDown()(见上文)中很好地获取诸如 MethodName 之类的 TestContext 数据,但无法获取 IEnumerable 中返回的方法名称。
我使用IEnumerable的测试用例如下:
[Test, TestCaseSource("GetTestData")]
public void LoginTest_E2E(JObject testData)
{
string loginId = testData["loginId"].Value<string>();
string password = testData["password"].Value<string>();
string useCase = testData["useCase"].Value<string>();
}
作为参考,我在测试中使用的.json数据文件是:
{
"LoginTest_E2E": [
{
"loginId": "testuser1",
"password": "password1",
"useCase": "verify for user 1"
},
{
"loginId": "tesetuser2",
"password": "password2",
"useCase": "verify for user 2"
}
]
有谁知道为什么我无法在IEnumerable 方法中获取testMethodName 的字符串值?
这里又是有问题的方法:
public static IEnumerable<JObject> GetTestData()
{
string testMethodName = TestContext.CurrentContext.Test.MethodName;
JObject[] testDataReturned = GetDataByMethod(testMethodName);
for (int i = 0; i < testDataReturned.Length; i++)
{
yield return testDataReturned[i];
}
}
【问题讨论】:
-
抱歉,从问题中不清楚您的 GetTestData 示例代码在测试中的位置以及如何调用它。请显示从测试中某个既定点开始的代码,例如 TestFixture 或 SetUpFixture。
-
谢谢@Charlie。我用更多细节更新了我的问题。让我知道你的想法。
标签: c# .net selenium nunit ienumerable