Adding unit tests to existing Android Studio project - android-studio

How do I add unit or integration tests to and existing Android Studio project?
I've tried adding folders in Project View as well as creating tests through the context menu (right-click).
I can't get Android Studio to recognize the folder as test folders.
I've also tried:
How to get Android Studio to recognize file as source (test)
Unit testing in android studio
Create unit tests in android studio

I can't get Android Studio to recognize the folder as test folders.
Following below folder structure, you should be able to see all your test cases labeled as "test" when select your Project View as "Android"
|- src
\-- androidTest // for android instrumentation tests
\-- main // your source code
\-- test // for unit tests

Adding a test directory labeled 'androidTest' is only recognized as a test directory if you already have directory 'test' under your src/ directory.
This is as of Android 3.1.3.

I had a similar issue, my Android TV had no 'androidTest' nor 'Test' directories, and I wanted to add some unit tests to the project.
What I did was to create an new Android project from scratch (regular app, not for TV), and I copied the test directory from the new project into my existing project (mantaining the folder structure that #shizhen mentioned above).
Finally I renamed the package folder's names and I got the ExampleUnitTest to run and work ok. After that, I could begin writing unit tests on my Android TV project.
Hope it Helps! It's probably not the best solution, but it works

Although posted answers are pretty straight forward, you may also try this auto generation plugin. This will handle everything for you and more, it'll also try to auto write test cases.
Use Unit Test Architect
Although TDD should be the approach for writing tests, but there may be a lot of untested code already written many times in larger projects.
One day I got frustrated with writing test cases of existing, older codebase. Hence, I thought of auto generating all the unit test cases.
I have created an Open-source Gradle Plugin which can be used for the above task. It is already hosted on mavenCentral. I have used it to generate test cases for my projects. But it can be used in any gradle project, (android, java, kotlin, kotlin+java). It may be rough around the edges but it has done it's job well for me.
BuildScript Dependency:
classpath "io.github.orange-3:unit-test-architect:$PLUGIN_VERSION"

Related

Why duplicate folders in an Android project?

I've found out that over a course of several years, a lot of programs keep seemingly duplicate "project folders" in the Android Studio, why is that?
To elaborate a bit further, if you import their projects, and if you take a look at there folder structure, there is going to be something like this:
Java
|--com.myproject.spaceInvader
|--com.myproject.spaceInvader(test)
|--com.myproject.spaceInvader(alphaTest)
What are these? Something generated by 3rd party testing tools?
When you create a project in Android Studio, it has a standard structure:
From official documentation:
main
Contains the "main" sourceset files: the Android code and
resources shared by all build variants (files for other build variants
reside in sibling directories, such as src/debug/ for the debug build
type). AndroidManifest.xml Describes the nature of the application and
each of its components. For more information, see the
AndroidManifest.xml documentation. java/ Contains Java code sources.
test
Contains code for local tests that run on your host JVM.
androidTest
Contains code for instrumentation tests that run on an
Android device. For more information, see the Android Test
documentation.

How to show dotCover coverage in new VS2017-type csproj project

I have a solution that contains 3 projects.
PROJECT 1
A VS2017-style .csproj project that has multitargetting to net45 and netstandard1.1.
PROJECTS 2 and 3
Two traditional VS2015-style .csproj test projects, targetting net45, using NUnit.
The reason for projects 2 and 3 not to be new-style is because it seems that right now ReSharper does not recognize these assemblies for running tests. It does find the tests just fine on old-style projects targeting any .NET framewory (full) or netcore assemblies, though.
Using that structure, I can run all my tests all right with the unit test window from dotCover, but when trying to gather coverage results it returns all green for the old-style projects, and all empty for the new-style project.
Coverage tree shows 0% coverage for my new-style project. Gutter highlighting and code highlighting all show in grey, although the tests have run successfully. All of them.
I've tried building in Debug mode, and setting specific values for "Platform" and "Framework" on the Unit Test Sessions window of ReSharper to no avail.
Is this something I'm doing wrong, or are we waiting for an update from ReSharper to fix this?
I am using:
NUnit 2.6.4 (because my unit test projects target net45)
Moq 4.5.28
ReSharper Ultimate 2017.1.2 with dotCover 2017.1.2 (says it's up to date at the time of writing).
Visual Studio Professional 2017 15.2 (26430.12)
I have installed all target frameworks until 4.7.02053
The projects I'm talking about are in C#
I had the same issue. And setting <DebugType>Full</DebugType> solved it for me as mentions by Martin

Prevent a project from building if a dependent project fails

I have a VS2010 solution that includes a custom project. The project builds an installer out of all the libraries and executables built in the same solution. This is a third-party installer builder, not a native Visual Studio installer project. It runs a custom command in the post-build event of the project.
The problem with this setup is that the installer project build runs (and succeeds!) even if some other project build fails.
I have added project dependencies so that the installer project depends on all other projects, to no avail. I have also tried to add project references with the same result.
This happens only with this specific project (probably because it's a custom project with configuration type "utility"). Other projects don't start building if a dependent project fails to build.
This is a dangerous situation. A developer may not pay attention to a build failure in some other project and use the incorrectly built installer. I need MSVC to skip building the installer project if some other project fails.
How should I deal with it?
Ideally this should be solved at the MSVC level. I don't want to add custom checks to the installer build command because this would mean I have to maintain a list of projects/targets in two separate places. I also don't want to introduce additional tools to the picture.

Android Studio: a lot of Gradle tasks missing after import

I have an Android Studio project that I imported in SVN. I didn't add some files like the build folder, local.properties, the .idea folder and .gradle folder, as suggested here. Then I checked out the project in another directory on my machine and imported it into Android Studio.
After the import, I tried to clean the project, but the option wasn't present in the GUI. I found it weird so I checked the list tasks with ./gradlew tasks and the ouput was indeed quite short (help tasks not shown):
Build Setup tasks
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
In the original project, here are a lot of other tasks (Android, build, install, verficitation and other tasks). Has anyone a clue on what is going on here ? The weird thing is that I just did the same procedure with another project and didn't get any problem.
Thank you
In my case there wasn't anything wrong with my settings.gradle file. If someone is still searching the solution, try to do these steps from my answer on similar question
Okay I figured out what the problem was.
After the import, the settings.gradle couldn't resolve some of the dependencies. I had some lines like
project(':apps').projectDir = new File(settingsDir, '../Libraries/apps')
Which I had to modify to
project(':apps').projectDir = new File(/home/path/to/workspace/Libraries/apps')
I will just leave to here if someone needs it.

Android Studio - How to open multiple project in single window?

I have downloaded Android Studio and started using it for my Android development.
I need to know, how to open multiple number of projects in a single window like Eclipse. Expecting some help, thanks.
IntelliJ IDEA creates a project for the entire code base you work with, and a module for each of its individual components. So, IntelliJ IDEA module is more like an Eclipse project, and project is roughly similar to Eclipse workspace. There's no exact equivalent to Eclipse's workspace that contains all your work, but you can open multiple projects in multiple frames at the same time.
This table can help you see how Eclipse and IntelliJ IDEA concepts map to each other:
Eclipse IDEA
Workspace Project
Project Module
Project-specific JRE Module JDK
User library Global library
Classpath variable Path variable
Project dependency Module dependency
Library Module library
To use the library add it as a dependancy:
File > Project Structure > Modules > Dependencies
Then add the module (android library) as a module dependency.
Open two projects in a single window is not possible in Android Studio / IntelliJ IDEA. So, when you open a second project, you'll have to decide:
New projects can either be opened in a new window or replace the project in the existing window.
How would you like to open the project?
This limitation is useful because your window offers project specific features, like the Changes tab for VCS information, etc.
How to use library projects?
For now, you can copy the library project into your project folder and declare it as a module dependency. If you use the same libraries in different projects, you will end up having the code multiple times.
ProjectA ProjectB
facebook-sdk/ actionbarsherlock/
actionbarsherlock/ bin/
bin/ src/
src/ ...
AndroidManifest.xml
While this feels kind of inconvenient, it helps having all the required sources in VCS. Soon, Gradle, the new build system, will manage these dependencies pleasantly. Here's an example of how the Gradle build could look like to include ActionBarSherlock or similar libs:
repositories {
mavenCentral()
}
dependencies {
compile 'com.actionbarsherlock:library:4.2.0'
}
In this answer you'll find some reasons why this solution does not work yet.
write code in settings.gradle
include ':ProjectName'
project(':ProjectName').projectDir = new File(rootDir, '/ProjectName')

Resources