在 JUnit4 中使用 @Override

发布于 2024-10-29 10:31:27 字数 916 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-11-05 10:31:27

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's java.lang.reflect.Method object, so you could just use the name of that Method object for your screenshot. It's not what you wanted, but it's better than nothing.

You could also call the getDeclaringClass() on the Method, to fetch the Class 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文