gpt4 book ai didi

c# - Nunit:如何在 NUnit 属性中有参数

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:06 24 4
gpt4 key购买 nike

我正在尝试调用带有 NUnit 属性的参数时收到错误

Invalid signature for SetUp or TearDown method: cleanup

我的脚本:

[Test]
public void Test()
{
TWebDriver driver = new TWebDriver();
driver.Navigate().GoToUrl("http://www.google.com");
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
string Name = methodBase.Name;
cleanup(Name);
}

[TearDown]
public void cleanup(string testcase)
{
string path = (@"..\..\Passor\");
DateTime timestamp = DateTime.Now;
if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
{
File.WriteAllText(Path.Combine(path, "Failed" + ".txt"), "Failed " + testcase);
}
else
{
File.WriteAllText(Path.Combine(path, "Passed" + ".txt"), "Passed " + testcase);
}
}

如果这是不可能的。有没有其他方法可以在清理方法中添加methodname

最佳答案

你不需要调用cleanup方法,它会被自动调用,你需要做的是将一些属性放在TestContext

例如:

使用字段:

[TestFixture]
public class GivenSomeTest
{
private string _testCase;
[Test]
public void Test()
{
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
_testCase = methodBase.Name;
TWebDriver driver = new TWebDriver();
driver.Navigate().GoToUrl("http://www.google.com");

}

[TearDown]
public void cleanup()
{
string path = (@"..\..\Passor\");
DateTime timestamp = DateTime.Now;
if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
{
File.WriteAllText(Path.Combine(path, "Failed" + ".txt"), "Failed " + _testCase);
}
else
{
File.WriteAllText(Path.Combine(path, "Passed" + ".txt"), "Passed " + _testCase);
}
}
}

使用TestContext:

[TestFixture]    
public class GivenSomeTest
{

[Test]
public void Test()
{
StackFrame stackFrame = new StackFrame();
MethodBase methodBase = stackFrame.GetMethod();
TestContext.CurrentContext.Test.Properties.Add("testCase",methodBase.Name);
TWebDriver driver = new TWebDriver();
driver.Navigate().GoToUrl("http://www.google.com");
}

[TearDown]
public void cleanup()
{
var testCase = TestContext.CurrentContext.Test.Properties["testCase"];
string path = (@"..\..\Passor\");
DateTime timestamp = DateTime.Now;
if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
{
File.WriteAllText(Path.Combine(path, "Failed" + ".txt"), "Failed " + testCase);
}
else
{
File.WriteAllText(Path.Combine(path, "Passed" + ".txt"), "Passed " + testCase);
}
}
}

关于c# - Nunit:如何在 NUnit 属性中有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com