public class

TraceRunListener

extends RunListener

 java.lang.Object

↳RunListener

↳androidx.test.internal.runner.listener.TraceRunListener

Gradle dependencies

compile group: 'androidx.test', name: 'runner', version: '1.5.0-alpha03'

  • groupId: androidx.test
  • artifactId: runner
  • version: 1.5.0-alpha03

Artifact androidx.test:runner:1.5.0-alpha03 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test:runner com.android.support.test:runner

Overview

A JUnit RunListener that reports info for each test case

Summary

Constructors
publicTraceRunListener()

Methods
public voidtestFinished(Description description)

public voidtestStarted(Description description)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public TraceRunListener()

Methods

public void testStarted(Description description)

public void testFinished(Description description)

Source

/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.test.internal.runner.listener;

import android.util.Log;
import androidx.tracing.Trace;
import org.junit.runner.Description;
import org.junit.runner.notification.RunListener;

/** A JUnit RunListener that reports {@link android.os.Trace} info for each test case */
public class TraceRunListener extends RunListener {

  private Thread startedThread = null;

  @Override
  public void testStarted(Description description) throws Exception {
    startedThread = Thread.currentThread();
    String testClassName =
        description.getTestClass() != null ? description.getTestClass().getSimpleName() : "None";
    String methodName = description.getMethodName() != null ? description.getMethodName() : "None";
    Trace.beginSection(testClassName + "#" + methodName);
  }

  @Override
  public void testFinished(Description description) throws Exception {
    if (Thread.currentThread().equals(startedThread)) {
      Trace.endSection();
    } else {
      // Trace expects the begin/end section calls to be on same thread.
      // Listeners should always be invoked on test/instrumentation thread,
      // but log an error in case this changes.
      Log.e("TraceRunListener", "testFinished called on different thread than testStarted");
    }
    startedThread = null;
  }
}