在 JUnit4 中使用 @Override
我正在使用 Selenium RC 和 JUnit 来编写测试。如果 @Test
失败,我想截取屏幕截图,但我想使用从 Test
类传递的特定屏幕截图名称。
目前我有:
@Rule
public ScreenshotRule email = new ScreenshotRule();
@Test
public void testLogin() throws InterruptedException {
MyTest someTest = new MyTest(selenium);
someTest.someMethod (); // if anything assert fails in this method, i want to take a shot and call it a name which i will pull from the MyTest class.
}
这是 Screenshot
类:
public class ScreenshotRule extends TestWatchman {
@Override
public void failed(Throwable e, FrameworkMethod method) {
System.out.println(method.getName() + " failed");
// At this point I wish to take a screenshot and call
}
}
那么如何将 String
arg 从 MyTest
类传递到 Screenshot< /代码> 类?
I am using Selenium RC and JUnit to write tests. If a @Test
fails, I would like to take a screen shot, but I want to use a specific screenshot name which is passed from the Test
class.
Currently I have :
@Rule
public ScreenshotRule email = new ScreenshotRule();
@Test
public void testLogin() throws InterruptedException {
MyTest someTest = new MyTest(selenium);
someTest.someMethod (); // if anything assert fails in this method, i want to take a shot and call it a name which i will pull from the MyTest class.
}
Here is the Screenshot
class :
public class ScreenshotRule extends TestWatchman {
@Override
public void failed(Throwable e, FrameworkMethod method) {
System.out.println(method.getName() + " failed");
// At this point I wish to take a screenshot and call
}
}
So how do I pass a String
arg from the MyTest
class to the Screenshot
class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TestWatchman
签名不允许您传递任何其他信息,因此您必须使用现有的信息。例如,
FrameworkMethod
类具有获取单元测试的java.lang.reflect.Method
对象的方法,因此您可以只使用名称用于屏幕截图的Method
对象。这不是你想要的,但总比没有好。您还可以在
Method
上调用getDeclaringClass()
来获取表示测试类的Class
,但您将无法获取该类的实例,这将很有用。PS我不确定
@Override
与您的问题有什么关系。它有何相关性?The
TestWatchman
signature does not permit you to pass any additional information, so you'll have to work with what you have.For example, the
FrameworkMethod
class has methods to fetch the unit test'sjava.lang.reflect.Method
object, so you could just use the name of thatMethod
object for your screenshot. It's not what you wanted, but it's better than nothing.You could also call the
getDeclaringClass()
on theMethod
, to fetch theClass
representing the test class, but you won't be able to fetch the instance of that class, which would be useful.P.S. I'm not sure what
@Override
has to do with your question. How is it relevant?