我正在为Android编写一些Espresso测试.我正在运行以下问题:
为了使某个测试用例正常运行,我需要在应用程序中禁用某些功能.因此,在我的应用程序中,我需要检测我是否正在运行Espresso测试,以便我可以禁用它.但是,我不想使用BuildConfig.DEBUG,因为我不希望在调试版本中禁用这些功能.此外,我想避免创建一个新的buildConfig,以避免创建太多的构建变体(我们已经定义了很多风格).
我一直在寻找一种方法来定义buildConfigField以进行测试,但我在Google上找不到任何引用.
更新了#1:更多信息添加到本文末尾
我是Android开发和测试的新手.
我有三个Espresso测试.第一次测试通过,但第二次测试不会运行,因为在第二次测试之前在setUp()方法中调用getActivity()总是失败:
Blockquote java.lang.RuntimeException:无法在45秒内启动意图Intent {act = android.intent.action.MAIN flg = 0x10000000 cmp = my.packagename/.ActivityMain}....
第三次测试通过.
我在创建时没有任何长时间运行的操作,动画或网络调用.我可以点击我的应用程序中的所有菜单项,手动重复测试流程而不会出现问题.
出于某种原因,第一次测试在第二次测试之前"中断"setUp()中的getActivity()调用.以下所有测试(第二次测试后)都将运行正常.
我发现了类似的问题,但看起来它没有解决,它有一点点不同的问题.
测试代码:
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onData;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static com.google.android.apps.common.testing.ui.espresso.action.ViewActions.click;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.isDisplayed;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import net.humblegames.bodylasticscalculator.ActivityMain;
import net.humblegames.bodylasticscalculator.R;
import net.humblegames.bodylasticscalculator.applogic.BandSystem;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
public class MenuNavigationTestExperiment extends …Run Code Online (Sandbox Code Playgroud)