如何使用浓缩咖啡测试为无线电组编写测试用例

时间:2019-04-16 08:49:49

标签: android kotlin android-espresso radio-group

我需要在android-kotlin中使用espresso编写无线电组的测试用例 下面是我用过的xml。

var arr = [89, 'sdf', 0, null, 0, true, 89, 0, 'sdf'];

function splitArray(arr, seperator, count) {
  let result = [], 
        copy = arr.slice(), // if you don't want to mutate the original array
        counter = 0;
  
  while(counter < count && copy.length > 0) {
    const index = copy.indexOf(seperator);
    if(index !== - 1) {
      result.push(copy.splice(0, index));
      copy.shift(); // remove the seperator
    }
    else result.push(copy.splice(0)) // add the remaining items

    counter++
  }
  
  return result
}

console.log(splitArray(arr, 0, 2))
console.log(splitArray(arr, 0, 3))

我尝试了一些代码

<RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:checkedButton="@+id/production"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/env_select"
            android:textStyle="bold"
            android:textSize="20sp"
            />

        <RadioButton
            android:id="@+id/production"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/env_production"
            />
        <RadioButton
            android:id="@+id/development"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/env_development"
            />
        <RadioButton
            android:id="@+id/testing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/env_testing"
            />
    </RadioGroup>

但是显示为 onView(withId(R.id.production)) .check(matches(isChecked())); onView(withId(R.id.development)) .check(matches(not(isChecked()))); onView(withId(R.id.testing)) .check(matches(not(isChecked())));

请帮助我编写相同的测试用例

1 个答案:

答案 0 :(得分:0)

您可以尝试这种新的测试方法:

    @Test
    fun checkRadioButtons() {
        // 1. Launching the activity with radio button
        ActivityScenario.launch(YourActivityName::class.java)

        // 2. Checking if radio group present
        onView(withId(R.id.radio_group))
                .check(matches(isDisplayed()))

        // You can also check for radio buttons similarly...

        // 3. Click one of the button and check
        onView(withId(R.id.production))
                .perform(click())

        onView(withId(R.id.production))
                .check(matches(isChecked()))

        onView(withId(R.id.testing))
                .check(matches(isNotChecked()))

        onView(withId(R.id.development))
                .check(matches(isNotChecked()))
    }

希望有帮助!