问题
We're trying to write instrumentationTest for our app.
For example I want to write test for authorization/registration.
However before every test I want to be unauthorized? so I need to clean all data, delete db, etc.
So is there any way to delete app or app's data before every test?
回答1:
Use Annotations eg:
@BeforeEach
and put your clean up code there.
回答2:
Using Android studio, you can create a test configuration that first uninstalls your app before running a test.
Create a new External Tool.
- Go to Preferences -> External Tools.
- Click on the "+" icon to create a new tool.
- Set Name to "Uninstall myApp" (or whatever you like)
- Set Group to "Custom Tools" (or whatever you like)
- Set Program to "adb"
- Set Parameters to uninstall
- Click OK.
You can test that this new tool works by clicking Tools->Custom Tools->"Uninstall myApp".
Step 2. Create or edit your test configuration.
- Go to Edit Configurations.
- Choose the Android Tests configuration that you'd like to modify.
- Under "Before launch: ...", click the "+" icon.
- Choose "Run External Tool".
- In the popup, choose your new tool "Uninstall myApp".
- Use the arrows so that the External Tool is above any other commands like Gradle-aware Make".
- Click Ok.
Now, when you run this configuration, your app should be uninstalled first, before your tests are run.
回答3:
This question was asked in '13. We have come a long way since then. Android has jUnit test runner that comes with a cleanup mechanism under test orchestrator.
Check it out here https://developer.android.com/training/testing/junit-runner
来源:https://stackoverflow.com/questions/20705711/uninstall-app-before-start-test