我已經查看了堆棧上的所有問題,我發現有類似的問題,但沒有人給我一個實際可行的答案(https://stackoverflow.com/search?q=espresso+empty+test+suite)。Espresso 2.1沒有找到我的測試
我有一個簡單的項目,我想嘗試意式濃縮咖啡。我的類文件看起來是這樣的:
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class FirstActivity extends Activity implements View.OnClickListener {
Button mClickMeButton;
TextView mHelloWorldTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setupView();
}
private void setupView() {
mHelloWorldTextView = (TextView) findViewById(R.id.helloWorldTextView);
mHelloWorldTextView.setText(getTitleText());
mClickMeButton = (Button) findViewById(R.id.clickMeBtn);
mClickMeButton.setOnClickListener(this);
}
private String getTitleText() {
return "First Activity";
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
我的測試文件是這樣的:
package com.example.myapplication.test;
import android.graphics.Bitmap;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.SmallTest;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import com.example.myapplication.BuildConfig;
import com.example.myapplication.FirstActivity;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class FirstActivityTest extends ActivityInstrumentationTestCase2<FirstActivity> {
public FirstActivityTest() {
super(FirstActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
@Test
public void testListGoesOverTheFold() {
onView(withText("First Activity")).check(matches(isDisplayed()));
}
}
和像這樣的build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
apply plugin: "sonar-runner"
apply plugin: 'spoon'
android {
compileSdkVersion 21
buildToolsVersion '22.0.1'
packagingOptions {
exclude 'LICENSE.txt'
}
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
testApplicationId "com.example.myapplication.test"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
testCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
testCompile 'org.easytesting:fest:1.0.16'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'com.squareup:fest-android:1.0.8'
androidTestCompile 'com.squareup.spoon:spoon-client:1.1.9'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
exclude group: 'javax.inject'
}
}
spoon {
debug = true
//devices = ['333236E9AE5800EC']
if (project.hasProperty('spoonClassName')){
className = project.spoonClassName
}
}
}
def coverageSourceDirs = [
'../app/src/main/java', 'src/gen'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files('build/jacoco/testDebug.exec')
}
}
每次我嘗試運行它,我得到:
Testing started at 16:57 ...
Target device: genymotion-google_nexus_5___5_0_0___api_21___1080x1920-192.168.56.101:5555
Uploading file
local path: /Users/pernillauhlin/development/pingdom/android-robolectric-test/app/build/outputs/apk/app-debug.apk
remote path: /data/local/tmp/com.example.myapplication
No apk changes detected. Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop com.example.myapplication
Uploading file
local path: /Users/pernillauhlin/development/pingdom/android-robolectric-test/app/build/outputs/apk/app-debug-androidTest-unaligned.apk
remote path: /data/local/tmp/com.example.myapplication.test
No apk changes detected. Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop com.example.myapplication.test
Running tests
Test running startedTest running failed: Unable to find instrumentation info for: ComponentInfo{com.example.myapplication.test/android.test.InstrumentationTestRunner}
Empty test suite.
我的結構是:
此外,從命令運行勺子給我相同的結果。所以,我的問題是:
- 我錯過了什麼?
- 測試註釋;在測試之前,我已經看到了許多變化,上課時間,等等。什麼是正確的方法?或者你可以在課前指定@LargeTest,在實際的測試方法之前指定@SmallTest?
編輯:
我的運行配置是這樣的:
它看起來像android.support.test.runner.AndroidJUnitRunner不存在。
構建變量:
在Android Studio中,轉到'Build Variants'並在'Test Artifact'中選擇'Android Instrumentation Tests'。 –
我已經有選擇。 – peuhse