我试图用NUnit属性调用一个参数我收到一个错误
签名
SetUp或TearDown方法无效: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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的.有没有其他方法可以添加methodname清理方法?
你不需要调用cleanup它会被自动调用的方法,你需要做的是把一些属性放在TestContext或者在一个字段中class.
例如:
[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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |