0

IInvokedMethodListener を実装する TestNG リスナーがあります。@BeforeMethod では、いくつかのテスト コンテキストを設定する必要があります。例を次に示します。

public class ThucydidesInvokedMethodListener implements IInvokedMethodListener2 {

    public void beforeInvocation(final IInvokedMethod method, final ITestResult testResult) {

    boolean areBeforeMethods = method.getTestMethod().getTestClass().getBeforeTestMethods().length > 0;
    if ((areBeforeMethods && method.getTestMethod().getTestClass().getBeforeTestMethods()[0] == method.getTestMethod()) ||
            !areBeforeMethods && method.isTestMethod()) {

        final ThucydidesTestContext context = new ThucydidesTestContext(testResult);
        testResult.setAttribute(contextKey(), context);
        context.before();
    }
} 

レポートでこのテスト名を使用するには、BeforeMethod の後に実行されるテスト名も必要です。これは TestNG を使用して可能ですか? また、追加で ITestContext を持つ IInvokedMethodListener2 を試しましたが、テスト名も提供していません。

4

3 に答える 3

2

テストを構成するためにリスナーを使用すると、私には間違っているように思えます-それが @Before* アノテーションの目的です。

リスナーで目的の情報を取得する方法はわかりませんが、 @BeforeMethod を使用すると簡単です。タイプ java.reflect.Method のパラメーターをメソッド シグネチャに追加するだけで、TestNG は現在のメソッドを挿入し、それを要求できます。その名前とあなたが知りたい他のすべて。

TestNG アノテーションのすべての「魔法」は、ここに文書化されています: TestNG 依存性注入

HTH

/イェンス

于 2013-02-18T08:52:55.217 に答える
0

さて、IInvokedMethodListener では、beforeInvocation メソッドが IInvokedMethod メソッドを提供します。

method.getTestMethod.getMethodName()メソッド名を与えます。

于 2014-02-20T18:09:42.537 に答える