зеркало из https://github.com/mozilla/MozStumbler.git
added docs for testing with Android Studio using the new test framework.
Updated test.sh to use simplified command line.
This commit is contained in:
Родитель
204d88e714
Коммит
81547ae3ef
|
@ -42,13 +42,17 @@ class Stuff {
|
|||
|
||||
}
|
||||
def resolveMethod(elementAtCaret) {
|
||||
def node = elementAtCaret;
|
||||
def METHOD_IMPL = "com.intellij.psi.impl.source.PsiMethodImpl"
|
||||
while (!node.getClass().getName().equals(METHOD_IMPL)) {
|
||||
node = node.parent;
|
||||
try {
|
||||
def node = elementAtCaret;
|
||||
def METHOD_IMPL = "com.intellij.psi.impl.source.PsiMethodImpl"
|
||||
while (!node.getClass().getName().equals(METHOD_IMPL)) {
|
||||
node = node.parent;
|
||||
}
|
||||
//show("Returning: "+ node.toString())
|
||||
return node;
|
||||
} catch (NullPointerException npe) {
|
||||
return null;
|
||||
}
|
||||
//show("Returning: "+ node.toString())
|
||||
return node;
|
||||
}
|
||||
|
||||
def runTestMethod(project, baseDir, testClassName, testMethodName) {
|
||||
|
@ -102,8 +106,15 @@ class Stuff {
|
|||
if (elementAtCaret != null) {
|
||||
msg += "\nCurrent Element: ["+elementAtCaret+"]"
|
||||
containingMethod = resolveMethod(elementAtCaret)
|
||||
if (containingMethod == null) {
|
||||
show("This doesn't look like a test method.")
|
||||
return
|
||||
}
|
||||
testMethodName = containingMethod.getName()
|
||||
if testMethodName
|
||||
if (testMethodName == null || !testMethodName.startsWith("test")) {
|
||||
show("This doesn't look like a test method.")
|
||||
return
|
||||
}
|
||||
msg += "\nCurrent method: ["+testMethodName+"]"
|
||||
|
||||
containingClass = resolveClass(containingMethod)
|
|
@ -7,10 +7,6 @@ work together.
|
|||
We use roboelectric to run fast unit tests within a regular JVM. This
|
||||
is not 100% problem free, but it allows your tests to run fast.
|
||||
|
||||
We use a Service Locator pattern
|
||||
(http://martinfowler.com/articles/injection.html#UsingAServiceLocator)
|
||||
to inject dependencies at runtime.
|
||||
|
||||
We are currently investigating the use of robotium to instrument the
|
||||
full client application to get more realistic tests that run on a real
|
||||
Android VM where good tests cannot be written using robolectric.
|
||||
|
@ -60,19 +56,106 @@ You will find an example there of loading a custom shadow to provide
|
|||
specialized behavior as well as the standard annotations you will need
|
||||
for your tests to run.
|
||||
|
||||
|
||||
After your tests are run, you will get an HTML report. If you run the
|
||||
testGithubUnittest target, your HTML output will appear in :
|
||||
|
||||
`android/build/test-report/github/unittest/index.html`
|
||||
|
||||
To run your tests, from the command line, the easiest thing to do is
|
||||
to use the test.sh shell script provided in the root directory of
|
||||
Mozilla Stumbler. Gradle expects that you to pass in a pattern
|
||||
matching the fully qualified classname post-fixed with the method you
|
||||
wish to test. So something like this ::
|
||||
|
||||
`./gradlew testGithubUnittest --tests --tests org.mozilla.mozstumbler.client.UpdaterTest.testUpdater`
|
||||
|
||||
The shell script uses a wildcard so that you can simply use:
|
||||
|
||||
`./test.sh UpdaterTest.testUpdater`
|
||||
|
||||
|
||||
Injecting Dependencies
|
||||
-----------------------
|
||||
|
||||
Mozilla Stumbler provides two mechanisms to inject dependencies into
|
||||
your code. Service Location is used primarily to decompose the
|
||||
application into logical components which are looked up using the
|
||||
ServiceLocator class. The Service Locator solves two problems,
|
||||
acquiring a reference to an implementation of a known interface, as
|
||||
well as being able to inject new implementations of that interface
|
||||
that dependant classes can start using.
|
||||
|
||||
We currently inject a clock service, as well as a simplified HTTP
|
||||
client. By doing this, we can change the system time while running
|
||||
under test by supplying an alternate test implementation of the
|
||||
ISystemClock. The IHttpClient interface has a similar role. The
|
||||
default implementation can be replaced as better APIs appear, but we
|
||||
can also replace the implementation when we run under test and stub
|
||||
out all network calls.
|
||||
|
||||
The second mechanism to inject dependencies, but this only works while
|
||||
running under test is to use [mockito](https://github.com/mockito/mockito).
|
||||
|
||||
Mockito provides two mechanisms to mock out classes, the mock()
|
||||
function and the spy() function. mock() can take a
|
||||
class, and return a usable implementation. This also conveniently
|
||||
works when passing in interface classes. mock() will provide a pure
|
||||
mock - a class that does a no-op for every method call.
|
||||
|
||||
Calling spy() on an instantiated object will allow you to override the
|
||||
methods of a live object with new methods. You can optionally also
|
||||
call the original method in your spy object. In most cases, this is
|
||||
probably what you want when writing tests as you can easily replace
|
||||
methods to do a no-op and focus on the part of your code that you are
|
||||
interested in.
|
||||
|
||||
|
||||
Some tips
|
||||
---------
|
||||
|
||||
It's helpful to read the code in the MapFragmentTest. That test case
|
||||
needs to instantiate a Fragment, have it run through the create
|
||||
portion of the activity life cycle.
|
||||
|
||||
In general, you probably want to minimize the length of methods which
|
||||
overload base android classes by pushing out most logic into a
|
||||
separate function so that you can use robolectric to run a fragment or
|
||||
activity through the creation phase of activity life cycle, but also
|
||||
stub out the portions of the create function that aren't important to
|
||||
the portion of code you are interested in testing.
|
||||
|
||||
Service Lookup
|
||||
--------------
|
||||
|
||||
To use the service lookup code, you want to look at the
|
||||
`org.mozilla.mozstumbler.svclocator` package. Using the service
|
||||
locator is relatively straight forward, we use a Service Locator
|
||||
pattern
|
||||
(http://martinfowler.com/articles/injection.html#UsingAServiceLocator)
|
||||
to inject dependencies at runtime.
|
||||
|
||||
The pattern that we use to add services is to use
|
||||
ServiceLocator::putService(<interface class>, implementation).
|
||||
|
||||
When you need to acquire a reference to a service, you call
|
||||
ServiceLocator::getService(<interface class>).
|
||||
|
||||
Android Studio integration
|
||||
--------------------------
|
||||
|
||||
TODO: Get instructions from here :
|
||||
We provide a Groovy live-plugin extension that can be used to run a
|
||||
single test method from within Android Studio 1.0 - the latest version
|
||||
at the time time this documentation was written. To use it, you will
|
||||
need to install the 'LivePlugin' plugin to Android Studio and install
|
||||
the plugin from `android_studio/plugin.groovy`.
|
||||
|
||||
http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/
|
||||
The video at http://youtu.be/y8lUv7YJexI demonstrates how to properly
|
||||
add this plugin.
|
||||
|
||||
Usage is very straight forward. Just focus your cursor inside the
|
||||
method you wish to test, and hit Ctrl-Shift-t. As long as the method
|
||||
you're in starts with the `test`, this should "just work". If it
|
||||
doesn't - please file a bug at https://github.com/mozilla/MozStumbler/issues
|
||||
|
||||
Simulation Mode
|
||||
---------------
|
||||
|
|
2
test.sh
2
test.sh
|
@ -7,4 +7,4 @@
|
|||
# See: https://github.com/JCAndKSolutions/android-unit-test for more
|
||||
# details.
|
||||
|
||||
./gradlew testGithubUnittest --tests org.mozilla.mozstumbler.client.UpdaterTest.testUpdater
|
||||
./gradlew testGithubUnittest --tests **$1
|
||||
|
|
Загрузка…
Ссылка в новой задаче