インターフェイス自動化テストに必要~テストの知識やスキル、対面体験は公式アカウントをチェック【テストメモ】
1. BeanShell と高度なグループの選択
testng.xml の <include> タグと <exclude> タグだけでは不十分な場合は、BeanShell 式を使用して、特定のテスト メソッドをテスト実行に含めるかどうかを決定できます。この式は、<test> タグの下に指定できます。
<test name="BeanShell test">
<method-selectors>
<method-selector>
<script language="beanshell"><![CDATA[
groups.containsKey("test1")
]]></script>
</method-selector>
</method-selectors>
<!-- ... -->
<script> タグが testng.xml で見つかると、TestNG は現在の <test> タグ内の後続のグループとメソッドの <include> と <exclude> を無視します。BeanShell 式はテスト メソッドが含まれるかどうかを一意に決定します。 。
BeanShell スクリプトに関する追加情報:
-
戻り値はブール値を返す必要があります。それ以外の有効な BeanShell コードは許可されます (たとえば、平日には true を返し、週末には false を返すと、日付に応じて異なるテストを実行できます)。
-
便宜上、TestNG では次の変数が定義されています。
-
java.lang.reflect.Method メソッド: 現在のテスト メソッド。
-
org.testng.ITestNGMethod testngMethod: 現在のテスト メソッドの説明。
-
java.util.Map<String, String> groups: 現在のテスト メソッドが属するグループのマップ。
-
-
(上記のように) CDATA 宣言で式を囲み、長い XML 予約文字の引用符を避けます。
2. アノテーションの変換
TestNG を使用すると、実行時にすべての注釈の内容を変更できます。通常の使用法は次のとおりです。注釈はほとんどの場合変更する必要はありませんが、値をオーバーライドする必要がある場合のみです。これを実現するには、アノテーション トランスフォーマー (Annotation Transformer) を使用する必要があります。
Annotation Transformer は、次のインターフェイスを実装するクラスです。
public interface IAnnotationTransformer {
/**
* This method will be invoked by TestNG to give you a chance
* to modify a TestNG annotation read from your test classes.
* You can change the values you need by calling any of the
* setters on the ITest interface.
*
* Note that only one of the three parameters testClass,
* testConstructor and testMethod will be non-null.
*
* @param annotation The annotation that was read from your
* test class.
* @param testClass If the annotation was found on a class, this
* parameter represents this class (null otherwise).
* @param testConstructor If the annotation was found on a constructor,
* this parameter represents this constructor (null otherwise).
* @param testMethod If the annotation was found on a method,
* this parameter represents this method (null otherwise).
*/
public void transform(ITest annotation, Class testClass,
Constructor testConstructor, Method testMethod);
}
他のすべての TestNG リスナーと同様に、このクラスはコマンド ラインまたは ant を使用して指定できます。
java org.testng.TestNG -listener MyTransformer testng.xml
または、エンコードされた形式で指定します。
TestNG tng = new TestNG();
tng.setAnnotationTransformer(new MyTransformer());
// ...
メソッド transform() が呼び出されると、TestNG プロセスが実行される前に、ITest テスト パラメータのセッター メソッドを呼び出してその値を変更できます。
次の例は、テスト クラスの 1 つの invoke() メソッドのみを呼び出す場合に、プロパティの invocationCount メソッドをオーバーライドする方法を示しています。
public class MyTransformer implements IAnnotationTransformer {
public void transform(ITest annotation, Class testClass,
Constructor testConstructor, Method testMethod)
{
if ("invoke".equals(testMethod.getName())) {
annotation.setInvocationCount(5);
}
}
}
IAnnotationTransformer では、1 つの @Test アノテーションのみを変更できます。別の TestNG アノテーション (構成アノテーション @Factory または @DataProvider など) を変更する必要がある場合は、IAnnotationTransformer2 を使用する必要があります。
3. メソッドインターセプター
TestNG がテスト メソッドが呼び出される順序を計算すると、メソッドは 2 つのグループに分割されます。
-
順次実行するメソッド。これらは、依存関係のあるテスト メソッドです。これらのメソッドは特定の順序で実行されます。
-
メソッドの実行順序は特に指定されません。これらは、最初のカテゴリに属さないメソッドです。これらのテスト メソッドが実行される順序はランダムであり、実行ごとに異なる場合があります (ただし、デフォルトでは、TestNG はクラスごとにテスト メソッドをグループ化しようとします)。
2 番目のカテゴリに属するメソッドをより適切に制御できるようにするために、TestNG は次のインターフェイスを定義します。
public interface IMethodInterceptor {
List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context);
}
渡されるメソッド リスト引数は、任意の順序で実行できるすべてのメソッドです。インターセプト メソッドは、次のいずれかの IMethodInstance の同様のリストを返すことが期待されます。
-
受信引数と同じメソッドリストですが、順序が異なります。
-
IMethodInstance リストよりも小さいメソッドのリスト。
-
IMethodInstance リストよりもわずかに大きいメソッド リスト。
インターセプターを定義したら、それをリスナーとして TestNG に渡します。例:
java -classpath "testng-jdk15.jar:test/build" org.testng.TestNG -listener test.methodinterceptors.NullMethodInterceptor
-testclass test.methodinterceptors.FooTest
「高速」グループに属するテスト メソッドを常に最初に実行するようにメソッドの順序を変更する メソッド インターセプターの例:
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
for (IMethodInstance m : methods) {
Test test = m.getMethod().getConstructorOrMethod().getAnnotation(Test.class);
Set<String> groups = new HashSet<String>();
for (String group : test.groups()) {
groups.add(group);
}
if (groups.contains("fast")) {
result.add(0, m);
}
else {
result.add(m);
}
}
return result;
}
私のWeChat公開アカウント[テストメモ]に注目して、より多くのテストスキルと知識をチェックしてください。一緒に学び、コミュニケーションしましょう~