こんにちは、JUnit テストは初めてです。
上から下に実行されず、ランダムに実行されるセレンコードを持つJUnitプログラムを実行します。
ただし、プログラムを順番に実行したいのですが、ログイン、作成、更新、削除などの機能があります。
でも、こんな感じで走っています。
このプログラムを順番に実行したい。あなたの貴重な提案を送ってください。
こんにちは、JUnit テストは初めてです。
上から下に実行されず、ランダムに実行されるセレンコードを持つJUnitプログラムを実行します。
ただし、プログラムを順番に実行したいのですが、ログイン、作成、更新、削除などの機能があります。
でも、こんな感じで走っています。
このプログラムを順番に実行したい。あなたの貴重な提案を送ってください。
テスト スイートを使用して、JUnitのクラスの順序を設定できます。
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestJunit1.class,
TestJunit2.class
})
public class JunitTestSuite {
}
@FixMethodOrder を使用してクラス内でテストの順序を設定します (JUnit 4.11 以降)
import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Test;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
@Test
public void firstTest() {
System.out.println("first");
}
@Test
public void secondTest() {
System.out.println("second");
}
}
注釈一覧
public interface AnnotationList{
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Order {
int value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTest {
static class None extends Throwable {
private static final long serialVersionUID = 1L;
private None() {
}}
JUNITLink クラス
public class JunitLink extends BlockJUnit4ClassRunner {
public JunitLink(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected List<FrameworkMethod> computeTestMethods() {
List<FrameworkMethod> classMethods = getTestClass().getAnnotatedMethods(AnnotationList.MyTest.class);
SortedMap<Integer, FrameworkMethod> sortedTestMethodList = new TreeMap<Integer, FrameworkMethod>();
for (FrameworkMethod seleniumTest : classMethods) {
if (seleniumTest.getAnnotation(AnnotationList.Order.class) != null) {
sortedTestMethodList.put(seleniumTest.getAnnotation(AnnotationList.Order.class).value(),seleniumTest);
}
}
return new ArrayList<FrameworkMethod>(sortedTestMethodList.values());
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
EachTestNotifier eachNotifier = makeNotifier(method, notifier);
if (method.getAnnotation(Ignore.class) != null) {
runIgnored(eachNotifier);
} else {
runNotIgnored(method, eachNotifier);
}
}
private int runNotIgnored(FrameworkMethod method,EachTestNotifier eachNotifier) {
int failures = 0;
eachNotifier.fireTestStarted();
try {
methodBlock(method).evaluate();
}
catch (AssumptionViolatedException e) {
eachNotifier.addFailedAssumption(e);
failures++;
}
catch (Throwable e) {
eachNotifier.addFailure(e);
failures++;
} finally {
eachNotifier.fireTestFinished();
}
return failures;
}
private void runIgnored(EachTestNotifier eachNotifier) {
eachNotifier.fireTestIgnored();
}
private EachTestNotifier makeNotifier(FrameworkMethod method,RunNotifier notifier) {
Description description = describeChild(method);
return new EachTestNotifier(notifier, description);
}
}
起動テスト
@RunWith(JunitLink.class)
public class StartUp extends SeleneseTestBase {
public static WebDriver driver;
@Override
@Before
public void setUp()
{
}
@Override
@After
public void tearDown() {
}
TestCases これは、上で作成した StartUp クラスを拡張する必要があります
public class Testcase extends StartUp{
public SmokeTest() throws Exception {
}
@Test
@Order(1)
// Wrire test method
}
@Test
@Order(2)
// Test Case 2
}
@Test
@Order(3)
//Test case 3
}