Cavy

Cavy

  • Documentation
  • API
  • GitHub
  • Roadmap
  • FAQ

›Cavy Native Reporter

Getting Started

  • Installing
  • Setting up the Cavy Tester
  • Writing tests

    • Hooking up components
    • Writing test cases
  • Running tests
  • Sample app

API reference

  • Tester Component
  • Test Hooks
  • Test Helpers
  • CLI Commands
  • Filtering tests
  • Cavy Native Reporter

Guides

  • Specifing a custom app entry point
  • Writing your own spec helpers
  • Writing your own custom reporter
  • Cavy Native Reporter

    • Using Cavy Native Reporter
    • iOS XCTest example (Obj-C)
    • iOS XCTest example (Swift)
    • Android example (JUnit)

Reporting to Android (JUnit)

To set up your own JUnit test that makes use of cavy-native-reporter:

  1. Open your project's android folder in Android Studio.
  2. Create a file for your instrumented Android tests at module-name/src/androidTest/java/. Switching to Project view in Android studio should help with this. Follow this link for more detailed instructions on setting up Instumented Android tests i.e. tests that run on an Android device or emulator.
  3. Add the following dependencies to android/app/build.gradle under dependencies (don't touch the build.gradle in the app folder itself!):
dependencies: {
  androidTestImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test:runner:1.1.0'
  androidTestImplementation 'androidx.test:rules:1.1.0'
  ...
}
  1. Add the following to android/app/build.gradle under defaultConfig:
defaultConfig {
  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  ...
}
  1. Write a test.

Taking the sample app as an example, we have an JUnit test BridgeTest which waits for Cavy tests to run and fails if any test returns an error:

package com.sampleapp.bridgetest;

import androidx.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.*;

import com.cavynativereporter.RNCavyNativeReporterModule;
// This should be the identifier for your own app's main activity.
import com.sampleapp.MainActivity;

public class BridgeTest {
  // This rule launches the main activity before each test annotated with @Test.
  @Rule
  public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class);

  @Test
  public void testBridge() throws Exception {
    // Wait 60 seconds to receive a test report from Cavy.
    RNCavyNativeReporterModule.waitForReport(60);
    // Pull the error count from the report object.
    double errorCount = RNCavyNativeReporterModule.cavyReport.getDouble("errorCount");
    // Note: Third argument is the `delta` allowed between the actual and
    // expected double value.
    assertEquals(0.0, errorCount, 0.0);
  }
}

See also

API reference

  • waitForReport
  • cavyReport

Useful links for Android Testing

  • Writing instrumented unit tests
  • Using JUnit4 Rules
  • Using the JUnit Runner
← iOS XCTest example (Swift)
Pixie Labs
Docs
Getting StartedGuidesAPI Reference
Community
HelpContributorsFurther reading
More
GitHub
StarFollow @pixielabsTweet
Cavy
Copyright © 2021 Pixie Labs