Android测试类中的测试方法没有分别运行。

huangapple go评论65阅读模式
英文:

android test methods in test class not run respectively

问题

我有一个测试类和其中的三个测试方法。问题是,当我运行测试时,具有@Test注解的方法不按它们在类中的顺序运行,首先运行getWorkPlanWhenNotReady(),然后是isWorkPlanReady(),最后运行loginIsSuccessfull()。我希望测试方法按它们在类中的顺序运行,任何帮助将不胜感激。这是我的测试类:

package com.tips.schoolbusdriverapp.largetests;

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;

/**
 * 执行在Android设备上的仪表化AppDataBase。
 *
 * @see <a href="http://d.android.com/tools/testing">测试文档</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);
        }
    }
}

希望这可以帮助您按顺序运行测试方法。

英文:

i have a test class and three test methods in it. problem is that when i run test, methods having @test annotation doest run in sequence they placed in class, first getWorkPlanWhenNotReady() runs then isWorkPlanReady() and last loginIsSuccessfull() runs. i want test methods run in sequence they placed in class , any help will be appereciated this is my test class:


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 &lt;a href=&quot;http://d.android.com/tools/testing&quot;&gt;Testing documentation&lt;/a&gt;
*/
@LargeTest
@RunWith(AndroidJUnit4ClassRunner.class)
public class MainScreenTest {
@Rule
public ActivityScenarioRule&lt;LoginTestActivity&gt; activityScenarioRule = new ActivityScenarioRule&lt;&gt;(
LoginTestActivity.class);
private IdlingResource loginIdlingResource;
private IdlingResource workPlanIdlingResource;
private LoginTestActivity loginTestActivity;
private MainActivity mainActivity;
@Test
public void loginIsSuccessfull() {
activityScenarioRule.getScenario().onActivity(activity -&gt; {
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(&quot;dri@gmail.com&quot;), closeSoftKeyboard());
Espresso.onView(withId(R.id.ed_password)).perform(clearText(), typeText(&quot;1&quot;), 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&lt;MainActivity&gt; activityRule
= new ActivityTestRule&lt;&gt;(
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

得分: 0

我通过在这个链接中找到的描述解决了这个问题:
按特定顺序运行测试

因为我使用JUnit运行器来运行测试,并使用以下注解来运行测试:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
并且将方法命名为所需的按字母顺序的名称解决了这个问题。

英文:

i resolved problem by finding a description in this link :
Running your tests in a specific order

and cause i use junit ruuner to run test this annotation :
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
and naming methods with desired alphabetical names solved the problem

huangapple
  • 本文由 发表于 2023年3月9日 16:34:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定