Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing Android and Permissions

I need android.permission.WRITE_SETTINGS for my Android application's junit tests. I am using IntelliJ Idea 11.1.2 and the unit tests in their own module, which contains second AndroidManifest.xml for the tests.

I added <uses-permission android:name="android.permission.WRITE_SETTINGS" /> to the test module's AndroidManifest.xml.

Error:java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=1063, uid=10035 requires android.permission.WRITE_SETTINGS

Ugly quick fix: I add the same permission also to the application's AndroidManifest.xml. Tests run without a single error.

The problem: I DO NOT want to give the whole application permission to change settings.

Is there a way to give permissions only for the tests?

like image 528
user1453173 Avatar asked Jun 13 '12 08:06

user1453173


People also ask

What is Android unit testing?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.

What is Android permission model?

Permission concept in Android. Android contains a permission system and predefined permissions for certain tasks. Every application can request required permissions. For example, an application may declare that it requires network access. It can also define new permissions.

How do I run unit test on Android?

To run all tests in a class or a specific method, open the test file in the Code Editor and do either of the following: Press the Run test icon in the gutter. Right-click on the test class or method and click Run . Select the test class or method and use shortcut Control+Shift+R .


1 Answers

Is there a way to give permissions only for the tests?

Short answer is no.

Your Android Test Project and Android Application Project are actually two applications, with test.apk instruments app.apk and run time. <uses-permission> is per application, added it to the test project's AndroidManifest.xml only gives required permission to test.apk, not the application under testing i.e. app.apk.

like image 86
yorkw Avatar answered Oct 06 '22 22:10

yorkw