Android单元测试不起作用

时间:2013-01-15 16:34:49

标签: android unit-testing

平,

我是Android测试新手。我尝试使用editText编写用于乘以两个数字的测试。它似乎没有运行,在模拟器上没有显示任何内容。我的测试代码如下

import android.test.ActivityInstrumentationTestCase2;

import com.example.calculator.MainActivity;
import com.jayway.android.robotium.solo.Solo;

public class MathValidation extends ActivityInstrumentationTestCase2<MainActivity> {

    private Solo solo;
    public MathValidation() {
        super(MainActivity.class);
    }

    protected void setUp() throws Exception {
        solo=new Solo(getInstrumentation(),getActivity());
    }

    protected void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }
    public void Test() throws Exception{
        solo.assertCurrentActivity("wrong activity", MainActivity.class);
        solo.enterText(0, String.valueOf("10"));
            solo.enterText(1,String.valueOf("30"));
        solo.clickOnButton("Multiply");
        assertTrue(solo.searchText("300"));


    }

这是我的logcat

01-15 16:23:27.832: E/Trace(1190): error opening trace file: No such file or directory (2)
01-15 16:23:27.832: W/Trace(1190): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:27.832: W/Trace(1190): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:27.832: W/Trace(1190): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:28.322: D/dalvikvm(1190): GC_CONCURRENT freed 331K, 15% free 2565K/3012K, paused 72ms+101ms, total 265ms
01-15 16:23:28.322: D/dalvikvm(1190): WAIT_FOR_CONCURRENT_GC blocked 12ms
01-15 16:23:28.713: D/dalvikvm(1190): GC_CONCURRENT freed 484K, 20% free 2540K/3144K, paused 73ms+85ms, total 215ms
01-15 16:23:28.862: W/Trace(1190): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:28.862: W/dalvikvm(1190): method Landroid/test/InstrumentationTestRunner$StringResultPrinter;.print incorrectly overrides package-private method with same name in Ljunit/textui/ResultPrinter;
01-15 16:23:31.202: E/Trace(1216): error opening trace file: No such file or directory (2)
01-15 16:23:31.212: W/Trace(1216): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:31.212: W/Trace(1216): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:31.212: W/Trace(1216): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:31.772: D/dalvikvm(1216): GC_CONCURRENT freed 334K, 16% free 2558K/3012K, paused 72ms+85ms, total 246ms
01-15 16:23:32.182: D/dalvikvm(1216): GC_CONCURRENT freed 491K, 18% free 2571K/3124K, paused 72ms+73ms, total 220ms
01-15 16:23:32.332: W/Trace(1216): Unexpected value from nativeGetEnabledTags: 0
01-15 16:23:32.332: W/dalvikvm(1216): method Landroid/test/InstrumentationTestRunner$StringResultPrinter;.print incorrectly overrides package-private method with same name in Ljunit/textui/ResultPrinter;

2 个答案:

答案 0 :(得分:0)

所有这些都是代码,而不是您实际运行测试的方式。您是从Eclipse还是从命令行运行它?无论如何,看看你所遵循的确切步骤会有所帮助。

此外,Solo是一项活动吗?

答案 1 :(得分:0)

在大多数测试框架中,测试方法的名称必须以test开头。方法名称区分大小写,因此问题是您的方法名为Test()而不是test()。这就是它没有运行的原因。

Java中的标准编码约定使用以小写字母开头的方法名称。此外,您通常希望为测试提供更具描述性的名称,例如: testMultiply()