在我的应用程序中,我使用 Kotlin Flow。之前我使用 挂起函数 EspressoIdlingResource.increment(),但它不适用于 Kotlin Flow。如何解决这个问题呢?
我是 Jetpack Compose 测试的新手,并试图弄清楚如何访问 an 的值OutlinedTextField以对它们执行 Instrumentation 测试:
我无法弄清楚访问和检查 EditField 的 SemanticsNode 中的某些值的语法。
我正在使用以下仪器测试:
@Test
fun NameTextField_LongInput_CompleteStatusAndLabelCorrect() {
composeTestRule.setContent {
ComposeTemplateTheme {
NameTextInput(name = "RandomName123", onNameInfoValid = { isComplete = it })
}
assertEquals(isComplete, true)
// This is accessing the label text
composeTestRule.onNodeWithText("Name").assertIsDisplayed()
//How do I access the Editable text?
//composeTestRule.onNodeWithEditableText("RandomName123") // How do I do something like this?!?!123
}
}
Run Code Online (Sandbox Code Playgroud)
我想弄清楚如何访问这棵树中的各个项目:
printToLog:
Printing with useUnmergedTree = 'false'
Node #1 at (l=0.0, t=110.0, r=1080.0, b=350.0)px
|-Node #2 at …Run Code Online (Sandbox Code Playgroud) android android-testing android-espresso android-jetpack-compose android-jetpack-compose-testing
我有一个活动,将从额外的东西中获取一个pojo,如下所示:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
MyPojo pojo = extras.getParcelable("pojo");
// do stuff with my pojo here
}
}
// button that shows a toast message
}
Run Code Online (Sandbox Code Playgroud)
我很难用Robolectric进行测试,我相信我需要使用ShadowIntents和ShadowActivities的组合,但是文档有点薄,而且关于这个特定场景的任何有用的教程都相当稀疏.
这是我到目前为止所提出的:
@Test
public void assertClickingTagSightingDisplaysToast()
{
myActivity = new MyActivity();
myActivity.onCreate(null);
ShadowActivity shadowMyActivity = shadowOf(myActivity);
shadowMyActivity.setIntent(new Intent().putExtra("pojo", generateAPojo()));
ShadowButton shadowButton = (ShadowButton) shadowOf(shadowMyActivity.findViewById(R.id.myButton));
shadowButton.performClick();
assertThat(ShadowToast.getTextOfLatestToast(), equalTo("Button was clicked!"));
}
Run Code Online (Sandbox Code Playgroud)
当我执行这些测试时,我一直得到一个空指针,在getIntent().getExtras()我的活动中,我假设我用意图模拟活动的过程是不正确的.
有人可以帮忙吗?
谢谢
tdd android robolectric android-2.3-gingerbread android-testing
我目前正在为我的项目实现Android的Fest,但我似乎遇到了依赖问题.如果我在没有包含Fest库的情况下运行测试,测试将正常运行.一旦我添加了Fest库,那么测试就不再运行了.而是抛出异常.
我的项目使用以下依赖项:
compile files('libs/robotium-solo-5.1.jar')
androidTestCompile 'com.squareup:fest-android:1.0.8'
androidTestCompile 'com.google.code.gson:gson:2.2.4'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.1'
androidTestCompile 'com.google.mockwebserver:mockwebserver:20130706'
androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.0') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
exclude module: 'mockito-core'
}
androidTestCompile 'org.mockito:mockito-all:+'
Run Code Online (Sandbox Code Playgroud)
我已经尝试排除我在下面列出的Fest Android依赖项,但它对测试的运行没有影响.
androidTestCompile ('com.squareup:fest-android:1.0.8') {
exclude group: 'com.google.android', module: 'android'
exclude group: 'com.google.android', module: 'support-v4'
exclude group: 'org.easytesting', module: 'fest-assert-core'
}
Run Code Online (Sandbox Code Playgroud)
这是在包含Fest库的情况下运行测试时发生的异常.
junit.framework.AssertionFailedError: Exception in constructor: testClickActionBarItems (java.lang.NoClassDefFoundError: com.example.android.activities.SectionsActivity
at com.example.android.test.activities.SectionsEspressoTests.<init>(SectionsEspressoTests.java:21)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:118)
at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:148)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:56)
at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:444)
at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:425)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onCreate(GoogleInstrumentationTestRunner.java:114) …Run Code Online (Sandbox Code Playgroud) android fest android-testing android-studio android-gradle-plugin
我在使用mockito在我的android应用程序中测试我的presenter类时遇到了一些奇怪的行为.
即使它通过我的设备,我的测试也会失败.行为不端的代码:
@Test
public void testValidPhoneNumber() throws Exception {
String phone = "0123456789";
assert(TextUtils.isDigitsOnly(phone));
}
Run Code Online (Sandbox Code Playgroud)
以上错误导致以下错误:
java.lang.AssertionError
at com.usehomeroom.vasuki.auth.RegisterPresenterTest.testValidPhoneNumber(RegisterPresenterTest.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 255
Run Code Online (Sandbox Code Playgroud) 当我添加依赖项时:
compile 'net.bytebuddy:byte-buddy-android:0.7.8'
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我收到此错误:
Conflict with dependency 'net.bytebuddy:byte-buddy'. Resolved versions for app (0.7.8) and test app (0.6.14) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Run Code Online (Sandbox Code Playgroud)
我访问了http://g.co/androidstudio/app-test-app-conflict,它说:
如果主APK和测试APK使用相同的库(例如Guava)但是在不同的版本中,Gradle构建将失败.[...]为了使构建成功,只需确保两个APK使用相同的版本
但我不知道这意味着什么.
你能帮助我吗?谢谢.
的build.gradle
...
buildTypes {
all {
//just build some config fields
}
demo.initWith(buildTypes.debug)
demo {
//just build some config fields
}
devel.initWith(buildTypes.debug)
devel {
//just build some config fields
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.gg
//just build some config fields
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false …Run Code Online (Sandbox Code Playgroud) 我需要测试我的自定义视图的状态是否正确保存在活动重新创建上.
我该如何测试呢?可以用Espresso进行测试吗?
因此,我最近开始在我现有的Android项目之一中搞混Espresso。
一切都进行得很顺利,直到我开始AutoCompleteTextView在程序中找到它。我似乎不明白如何正确单击自动完成列表中的第一件事。实际上,我甚至不能确定使用哪个,onView()或者onData()在这种情况下。
android autocompletetextview android-testing android-espresso
在我的应用程序中,我在一个活动中附加了两个片段。每个片段包含不同的视图。
第一个具有两个编辑文本字段和两个按钮,用户可以在其中登录或注册。所以现在我想进行一些Espresso测试。
@RunWith(AndroidJUnit4.class)
public class LoginTest {
@Rule
public final ActivityRule<MainActivity> main = new ActivityRule<>
(MainActivity.class);
@Test
public void shouldBeAbleToFindViewsOfLoginScreen(){
onView(withText(R.id.user_name)).perform(click());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,测试抱怨它找不到ID为R.id.user_name的视图。
android.support.test.espresso.NoMatchingViewException: No views in hierarchy
found matching: with string from resource id: <2131493050>[user_name] value:
false
Run Code Online (Sandbox Code Playgroud)
我也使用了从uiautomatorviewer检查的相应ID。
该片段的xml代码是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentLogin"
tools:context="team.football.ael.MainActivity"
android:background="@drawable/background">
<include
android:id="@+id/toolbar"
layout="@layout/tool_lay"
/>
<LinearLayout
android:padding="20dp"
android:background="@drawable/roundedlayout"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:src="@mipmap/ael_logo"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<EditText
android:id="@+id/user_name"
android:hint="@string/login_username"
android:textColor="#fff"
android:textColorHint="#fff"
android:layout_width="250dp" …Run Code Online (Sandbox Code Playgroud) 请帮我这个,当我一起运行所有类测试方法时,测试失败的方法虽然这个方法,当我单独运行它成功
public class RealEstatesListPresenterTest {
RealEstatesListPresenter mRealEstatesListPresenter;
@Mock
private RealEstateListBusiness mRealEstateListBusiness;
@Mock
private RealEstatesListContract.View mRealEstatesView;
@BeforeClass
public static void setUpClass() {
RxAndroidPlugins.setInitMainThreadSchedulerHandler(__ -> Schedulers.trampoline());
}
@Before
public void setupTasksPresenter() {
MockitoAnnotations.initMocks(this);
mRealEstatesListPresenter = new RealEstatesListPresenter(mRealEstatesView);
mRealEstatesListPresenter.setmRealEstateListBusiness(mRealEstateListBusiness);
}
@Test
public void testWhenGetAllRealEstates_ProgressISDisplayed() {
when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
sub.onNext(new ArrayList<>());
sub.onComplete();
}));
mRealEstatesListPresenter.getAllRealEstates();
verify(mRealEstatesView, times(1)).showLoading();
}
@Test
public void testWhenGetAllRealEstatesSuccess_ProgressISHidden() {
when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
sub.onNext(new ArrayList<>());
sub.onComplete();
}));
mRealEstatesListPresenter.getAllRealEstates();
verify(mRealEstatesView, times(1)).hideLoading();
}
@Test
public void testWhenGetAllRealEstatesError_ProgressISHidden() {
when(mRealEstateListBusiness.getAllRealEstates()).thenReturn(Observable.create(sub -> {
sub.onError(new Throwable());
})); …Run Code Online (Sandbox Code Playgroud) android-testing ×10
android ×9
mockito ×2
android-jetpack-compose-testing ×1
fest ×1
gradle ×1
kotlin-flow ×1
robolectric ×1
rx-android ×1
tdd ×1
ui-testing ×1
unit-testing ×1