昨天我们探究了 UI 自动化中代码覆盖率,今天我们来看看如何实现功能测试人员测试过程中,代码覆盖率的计算
昨天我们使用 jacoco,在 build.gradle 加入了很多代码:
apply plugin: 'jacoco'
....
jacoco{
toolVersion = "0.7.1.201405082137"
}
android {
buildTypes {
debug {
testCoverageEnabled = true
}
今天验证了一下,上面的代码只需要一个testCoverageEnabled = true
其他都不需要添加,就可以使用 jacoco,我们来实验一下:
试验证明,gradle 为 android 提供的插件默认使用的是 jacoco,所以你只需要将 testCoverageEnabled 设置为 true,一切就都搞定了。
现在我们来回到正题,就是如何在手工测试的过程中收集代码覆盖率呢?首先我们试试 Monkey 跟我说的,还是用 Instrumentation 的 case 启动应用,然后在 case 中 sleep 一段时间,在这段时间中,我们手动去点击控件,来看能否收集到代码覆盖率。
为了方便观察,我在主 activity 中添加一个 button,点击 button 可以跳转到 GoActivity。
MainActivity:
package com.wuba.wuxian.android_0504;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button goButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
goButton = (Button) findViewById(R.id.button);
goButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,GoActivity.class);
startActivity(intent);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GoActivity:
package com.wuba.wuxian.android_0504;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class GoActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_go);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_go, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wuba.wuxian.android_0504" >
<!-- To access Google+ APIs: -->
<uses-permission android:name="android.permission.INTERNET" />
<!--
To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect
option is required to comply with the Google+ Sign-In developer policies
-->
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".GoActivity"
android:label="@string/title_activity_go" >
</activity>
</application>
</manifest>
我们的 Robotium 脚本如下:
public class MainActivityTest extends ActivityInstrumentationTestCase2 {
private Solo solo;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void testStartClose() throws Exception {
Thread.sleep(1000 * 10);
}
// public void testClickButton(){
// solo.clickOnText("Go");
// }
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
}
我们分两种方式获取代码覆盖率
结果可以看出来,我们的代码覆盖率是 42%
我们的代码覆盖率达到了 74%,jacoco 也统计了我手工点击的操作。
虽然实现了统计手工测试的代码覆盖率,但是还是得依赖 instrumentation,而且时间还有限制,我们在 Robotium 的 case 中设置的时间是 10s,意思 10s 内的操作会统计,结束以后就不统计了。这就无法自由的测试了。所以这种方法还是不好,明天试试思寒大哥的方法。
(先去踢球了)