Pages

Tuesday, September 29, 2015

Testing with Android Studio

Tried to write this in the order one would discover how to configure and test in Android Studio. Test support has come in many forms with different purposes in AS(Android Studio). There are/were plugins and various 3rd pty libs. AS now supplies its own test platform. None of which is configured to fly right out of the box.

Setup

Get the Android Support Library from SDK manager. Recently the manager has been moved to the setting interface. You can still get to the old sdk interface.
Testing Support
Verify you have the libs in your sdk folder.
<sdk>/extras/android/m2repository/com/android/support/test

Create a new project to test with.

Create a new blank activity app.
  • compile app
  • open the default MainActivity class
Gradle plugin testInstrumentationRunner needs overriding.
  • Manually change the build.gradle file. If you search the gradle site for testInstrumentationRunner you will see the default is “android.test.InstrumentationTestRunner”.
  • Wasn’t able to find UI way to change this value.
  • This will come in handy as it supplies a value for the debug/run config interface.
defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
Open create test interface
  • right click on the MainActivity class name -> Go To -> Test
  • CTRL + SHIFT + T
Configure the test interface
  • select JUnit4 and Fix (what does fix do?)
  • select one or more methods to test and before after checks
  • should create import statments and test methods in test class
choose the destination test directory interface
  • the one that contains “src” in the path
  • this can be a larger list if you have other modules in your project
Should have a new file MainActivityTest in the test directory.
  • The build.gradel file still does not have reference to the important libs we are going to use “com.android.support.*”
Apply the first annotation that requires reference
  • Add @RunWith(AndroidJUnit4.class) to test class
  • Alt + Enter @RunWith to create import
  • AndroidJUnit4 class reference not found. Dependencies missing.
Add android.support.test.* dependencies to the project
  • modifying build.gradle is can be done via the project structure interface. (or manually)
  • select your app right click and select "open module setting (F4)
  • select the module app and choose the dependicies tab
  • find the + - buttons + add the “library dependency”
  • paste in the full lib name in the “choose library dependency” interface. It will not work with a partial match.
  • change the “scope” to Test compile.
  • OK, clean, compile
  • you should see the new references in the (app)build.gradle file.
Here are the latest libs for test support. They will change.
'com.android.support.test:runner:0.4'
'com.android.support.test:rules:0.4'
'com.android.support.test.espresso:espresso-core:2.2.1'
'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
'com.android.support:support-annotations:23.0.1'
Others
org.hamcrest:hamcrest-library:1.3
Add the import reference AndroidJUnit4.class
  • dependency should now exist for AndroidJUnit4.class
  • Alt + Enter AndroidJUnit4 to create import
Setup one test to Pass
  • we will use assertThat from hamcrest.org
  • add this test method
@Test
public void DonkeyTest() throws Exception {
    assertThat("", is(""));
}
  • Alt + Enter “assertThat” to create import
  • Alt + Enter “is” to create import
Try running the test
  • Right click on test class file. Should see Create, debug, run
  • Select Create to generate a run configuration
  • The “Specific Instrumentation runner(optional):” is not optional. Should be already set to “android.support.test.runner.AndroidJUnitRunner”
  • if not select … and choose “android.support.test.runner.AndroidJUnitRunner”
  • Apply, Ok
  • Right click again and select run/debug
  • If you goto “Edit Configurations” you can delete what was made in this step.
Locate the results
  • Run dialog should show the Result / Command / Test Method

Conclusions

Getting an idea of where all the moving parts are is a good first step. Primary suspects is the presense of the testing libraries and the correct configuration of the build.gradle.
There are a lot of new api introduced which can be overwhelming. Hard to tell which is the current official storyline.
Google is making attempts at gathering this config stuff in one spot with Android Testing Blueprint

Links

Friday, September 11, 2015

Virtualenv

Arriving at the conclusion that you need virtualenv you quickly enter into how_where_what land.  Your why is because you want multiple versions of python and to pip packages to your hearts content.

This site virtualenv sums it up pretty well but is kind of light on the details.  After reading it you start wandering around for more answers.   Each environment kind of has its own tricks and traps.

I wanted to know how-does-virtualenv-work without reverse-engineering-ian-bicking.  Final answer is: this shell these paths please. 

Before getting into virtualenv you would want to know more about "pip" (package manager).   Pip is one of a set of tools that put the python packages into the python user space.  

So don't forget that the virtualenv command just populates a directory with all the folders and symlinks to the python binaries you want.  

activate and deactivate command exist in reference to the environment that was setup by virtualenv.  They engage and disengage those paths that now exist as directories in your directory.

Install

sudo apt-get install python-dev python-pip python-virtualenv

Setup

Really good rundown on setting up. run-multiple-python-versions-on-your.  Pay attention to the how he manages his environments and the use of ~/.bash_alias

Links



how-does-virtualenv-work

Friday, May 1, 2015

Ubuntu 14 Android Studio setup notes


Setting up Android Studio on Ubuntu requires many extra packages. Here are some notes on setting up the latest versions. Special note that you want a CPU that supports the android emulator. I used an AMD with graphics support.

-------------------
#requires java 7

sudo apt-get install default-jdk

#getting the packages

mkdir Android

cd Android

wget http://dl.google.com/android/android-sdk_r24.0.2-linux.tgz

tar -xvzf android-sdk_r24.0.2-linux.tgz

wget https://dl.google.com/dl/android/studio/ide-zips/1.1.0/android-studio-ide-135.1740770-linux.zip

unzip android-studio-ide-135.1740770-linux.zip

#add $PATH to end of  ~/.bashrc file

export PATH=$PATH:~/Android/android-studio/bin

#required packages 

sudo dpkg --add-architecture i386
sudo apt-get update

sudo apt-get install libncurses5:i386 libstdc++6:i386 zlib1g:i386

#setup the KVM (virtual machine)

#verify kvm is funtional before running android studio setup.

#http://sharadchhetri.com/2014/10/09/install-kvm-kernel-based-virtual-machine-ubuntu-14-04-lts-desktop/

#https://help.ubuntu.com/community/KVM/Installation

sudo apt-get install qemu-kvm libvirt-bin bridge-utils
sudo apt-get install virt-manager
sudo apt-get install qemu-system

#logout or reboot

#start studio by opening termainal and run studio.sh

#update the sdk and add emulators


https://gist.github.com/anonymous/d305c68ea5d182a20f06#file-android_studio_on_ubuntu