screenshot-tests-for-android icon indicating copy to clipboard operation
screenshot-tests-for-android copied to clipboard

How to run specific test cases in a specific class?

Open MohamedMedhat1998 opened this issue 4 years ago • 2 comments

I have a class that contains screenshot-tests with the following code:

class ScreenshotTestingTests {

    @get:Rule
    var activityTestRule = ActivityTestRule(MainActivity::class.java)

    @Test
    fun testHomeActivity() {
        val intent = Intent()
        val activity = activityTestRule.launchActivity(intent)
        Screenshot.snapActivity(activity).record()
    }
}

and another class that contains normal instrumented tests with the following code:

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.andalus.screenshottestingdemo", appContext.packageName)
    }

    @Test
    fun testMainActivity_buttonHasCorrectId() {
        ActivityScenario.launch(MainActivity::class.java).use {
            onView(withId(R.id.button)).check(matches(withText("BUTTON")))
        }
    }

    @Test
    fun testMainActivity_buttonHasCorrectId1() {
        ActivityScenario.launch(MainActivity::class.java).use {
            onView(withId(R.id.button)).check(matches(withText("BUTTON")))
        }
    }

    @Test
    fun testMainActivity_buttonHasCorrectId2() {
        ActivityScenario.launch(MainActivity::class.java).use {
            onView(withId(R.id.button)).check(matches(withText("BUTTON")))
        }
    }

    @Test
    fun testMainActivity_buttonHasCorrectId3() {
        ActivityScenario.launch(MainActivity::class.java).use {
            onView(withId(R.id.button)).check(matches(withText("BUTTON")))
        }
    }

    @Test
    fun testMainActivity_buttonHasCorrectId4() {
        ActivityScenario.launch(MainActivity::class.java).use {
            onView(withId(R.id.button)).check(matches(withText("BUTTON")))
        }
    }

    @Test
    fun testMainActivity_buttonHasCorrectId5() {
        ActivityScenario.launch(MainActivity::class.java).use {
            onView(withId(R.id.button)).check(matches(withText("BUTTON")))
        }
    }
}

When I try to run recordDebugAndroidTestScreenshotTest, runDebugAndroidTestScreenshotTest, or verifyDebugAndroidTestScreenshotTest, Screenshot (262) all the test cases are started and I get in the log

Starting 8 tests on Google Pixel C - 7.1.1

I want only to start the "screenshot-related" tests only.
Is this possible?

MohamedMedhat1998 avatar Aug 21 '20 12:08 MohamedMedhat1998

./gradlew :app:verifyDebugAndroidTestScreenshotTest -Pandroid.testInstrumentationRunnerArguments.class=full_package_name.ClassName#methodName

You can change :app: by your module name, and verify to record or other tasks.

malkes avatar Aug 31 '20 21:08 malkes

@malkes when you run the above command on a given testclass or testclass.method, using record instead of verify, it seems to delete all previously recorded screenshots locally.. is there a way to have it not delete other screenshots?

eliddell1 avatar Feb 24 '22 14:02 eliddell1