Deleting files for android espresso test

1.4k views Asked by At

I have a MainMenu activity that gets data from file and displays it. I want the data to be deleted at the start of each espresso test and to not persist between tests.

I have tried the following:

Context mContext;

@Before
public void setUp() {
    mContext = InstrumentationRegistry.getInstrumentation().getContext();
    File[] files = mContext.getFilesDir().listFiles();
    if(files != null){
        for(File file : files) {
            file.delete();
        }
    }
}

However, it is not deleting the files. I believe the context might not be correct. Is there a way to clear internal storage at the start of an espresso test?

The files are '.ser' files.

3

There are 3 answers

0
Renetik On

In kotlin..

@BeforeClass
fun beforeClass() {
    getInstrumentation().targetContext.filesDir.deleteRecursively()
}

@RunWith(AndroidJUnit4::class)
@LargeTest
class SomeTest {
    
    @Test
    fun someFreshInstallationTest() {
        ...
    }
    
}
1
Gerardo Rodriguez On

I ran into the same problem. The solution was only to change getContext() for getTargetContext()

mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    File[] files = mContext.getFilesDir().listFiles();
    if(files != null){
        for(File file : files) {
            file.delete();
        }
    }

As simple as that :)

0
Kevin VanDenBreemen On

Have you tried using adb shell commands?

One thing I tried that was helpful in deleting files in an Espresso test was executing ADB shell commands programmatically. Something like this:

(Kotlin sample)

val dir = Environment.getExternalStorageDirectory()
    val file = File(dir.absolutePath
            + File.separator + directoryInExtStorageDir
            + File.separator + fileName)
    InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
            "rm -f " + file.absolutePath)

Java would look something like this:

File dir = Environment.getExternalStorageDirectory()
    File file = new File(dir.getAbsolutePath()
            + File.separator + directoryInExtStorageDir
            + File.separator + fileName)
    InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
            "rm -f " + file.getAbsolutePath())

As for seeking out files of a specific type you can use the FilenameFilter or FileFilter interfaces for that. See this answer and this page for examples of how you can do this.

An example might look like this:

File dir = Environment.getExternalStorageDirectory();
    File directory = new File(dir.getAbsolutePath()
            + File.separator + directoryInExtStorageDir);
    List<File> filesToDelete = Arrays.asList(directory.listFiles((new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".ser");
        }
    })));

    filesToDelete.forEach(file->{
        InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(
                "rm -f " + file.getAbsolutePath());
    });

Hope that helped.