要生成自定义测试报告,你可以使用以下步骤:
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
public class CustomTestReporter extends RunListener {
@Override
public void testStarted(Description description) throws Exception {
System.out.println("Test Started: " + description.getMethodName());
}
@Override
public void testFinished(Description description) throws Exception {
System.out.println("Test Finished: " + description.getMethodName());
}
@Override
public void testFailure(Failure failure) throws Exception {
System.out.println("Test Failed: " + failure.getDescription().getMethodName());
System.out.println("Error Message: " + failure.getMessage());
}
@Override
public void testRunFinished(Result result) throws Exception {
System.out.println("Total Tests Run: " + result.getRunCount());
System.out.println("Total Tests Failed: " + result.getFailureCount());
}
}
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.rule.ActivityTestRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(MainActivity.class);
@BeforeClass
public static void setUpClass() {
// 在测试套件运行前的初始化操作
}
@AfterClass
public static void tearDownClass() {
// 在测试套件运行后的清理操作
}
@Test
public void exampleTest() {
// 你的测试代码
}
}
import org.junit.Rule;
import org.junit.runner.RunWith;
import androidx.test.rule.ActivityTestRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Rule
public CustomTestReporter mTestReporter = new CustomTestReporter();
@Test
public void exampleTest() {
// 你的测试代码
}
}
这样,你就可以在运行测试时生成自定义的测试报告了。你可以根据需要对CustomTestReporter类进行修改,以满足你的具体需求。