Home | History | Annotate | Download | only in junit
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.cts.junit;
     18 
     19 import org.junit.runner.JUnitCore;
     20 import org.junit.runner.Request;
     21 import org.junit.runner.Result;
     22 
     23 /**
     24  * Test runner to run a single JUnit test. It will output either [PASSED] or [FAILED] at the end.
     25  */
     26 public class SingleJUnitTestRunner {
     27     private static String mUsage = "Usage: java -cp <classpath> SingleJUnitTestRunner" +
     28             " class#testmethod";
     29     private static final String PASSED_TEST_MARKER = "[ PASSED ]";
     30     private static final String FAILED_TEST_MARKER = "[ FAILED ]";
     31 
     32     public static void main(String... args) throws ClassNotFoundException {
     33         if (args.length != 1) {
     34             throw new IllegalArgumentException(mUsage);
     35         }
     36         String[] classAndMethod = args[0].split("#");
     37         if (classAndMethod.length != 2) {
     38             throw new IllegalArgumentException(mUsage);
     39         }
     40         Request request = Request.method(Class.forName(classAndMethod[0]),
     41                 classAndMethod[1]);
     42         JUnitCore jUnitCore = new JUnitCore();
     43         Result result = jUnitCore.run(request);
     44         String status = result.wasSuccessful() ? PASSED_TEST_MARKER : FAILED_TEST_MARKER;
     45         System.out.println(String.format("%s %s.%s", status,
     46                 classAndMethod[0], classAndMethod[1]));
     47     }
     48 }
     49