我有一个测试类和三个测试方法。问题是,当我运行测试时,具有@test注解的方法不按它们在类中放置的顺序运行,首先运行getWorkPlanWhenNotReady(),然后运行isWorkPlanReady(),最后运行loginIsSuccessfull()。我希望测试方法按它们在类中放置的顺序运行,将出现任何帮助这是我的测试类:
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.IdlingResource;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.filters.LargeTest;
import androidx.test.filters.MediumTest;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import com.tips.schoolbusdriverapp.R;
import com.tips.schoolbusdriverapp.data.models.LoginPojo;
import com.tips.schoolbusdriverapp.data.models.MainModel;
import com.tips.schoolbusdriverapp.ui.login.LoginActivity;
import com.tips.schoolbusdriverapp.ui.login.LoginTestActivity;
import com.tips.schoolbusdriverapp.ui.login_easy.EasyLoginTestActivity;
import com.tips.schoolbusdriverapp.ui.main.MainActivity;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static androidx.test.espresso.action.ViewActions.clearText;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.assertThat;
import static androidx.test.espresso.matcher.ViewMatchers.doesNotHaveFocus;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Instrumented AppDataBase, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@LargeTest
@RunWith(AndroidJUnit4ClassRunner.class)
public class MainScreenTest {
@Rule
public ActivityScenarioRule<LoginTestActivity> activityScenarioRule = new ActivityScenarioRule<>(
LoginTestActivity.class);
private IdlingResource loginIdlingResource;
private IdlingResource workPlanIdlingResource;
private LoginTestActivity loginTestActivity;
private MainActivity mainActivity;
@Test
public void loginIsSuccessfull() {
activityScenarioRule.getScenario().onActivity(activity -> {
loginTestActivity = activity;
loginIdlingResource = activity.getEasyLoginIdlingResource();
IdlingRegistry.getInstance().register(activity.mIdlingResource);
});
Context appContext = InstrumentationRegistry.getInstrumentation().getContext();
Espresso.onView(withId(R.id.ed_email)).perform(clearText(), typeText("dri@gmail.com"), closeSoftKeyboard());
Espresso.onView(withId(R.id.ed_password)).perform(clearText(), typeText("1"), closeSoftKeyboard());
Espresso.onView(withId(R.id.login))
.perform(click());
LoginPojo loginPojo = loginTestActivity.getLoginResponse();
if (loginPojo.isSuccess()) {
activityScenarioRule.getScenario().moveToState(Lifecycle.State.DESTROYED);
runMainActivity(appContext);
}
else assertTrue(loginPojo.isSuccess());
}
@Test
public void isWorkPlanReady() {
Espresso.onView(withId(R.id.retryWP2)).check(doesNotExist());
}
@Test
public void getWorkPlanWhenNotReady() {
Espresso.onView(withId(R.id.retryWP2))
.perform(click());
Espresso.onView(withId(R.id.retryWP2)).check(doesNotExist());
}
public void runMainActivity(Context context) {
ActivityTestRule<MainActivity> activityRule
= new ActivityTestRule<>(
MainActivity.class,
true,
false);
Intent intent = new Intent();
activityRule.launchActivity(intent);
mainActivity=activityRule.getActivity();
workPlanIdlingResource=activityRule.getActivity().mIdlingResource;
IdlingRegistry.getInstance().register(workPlanIdlingResource);
}
@After
public void unregisterIdlingResource() {
if (loginIdlingResource != null) {
IdlingRegistry.getInstance().unregister(loginTestActivity.mIdlingResource);
}
if(workPlanIdlingResource!=null){
IdlingRegistry.getInstance().unregister(mainActivity.mIdlingResource);
}
}
}
1条答案
按热度按时间rekjcdws1#
我通过在此链接中找到描述解决了问题:Running your tests in a specific order
并导致我使用junit ruuner来运行测试此注解:@FixMethodOrder(MethodSorters.NAME_ASCENDING)和使用所需字母名称命名方法解决了这个问题