尝试生成一个简单的ActivityUnitTestCase来测试Android代码中的各种片段。
public class MenuFragmentTest extends ActivityUnitTestCase<FragmentAdapter> {
static {
FragmentAdapter.setFragmentUnderTest(new MenuFragment());
}
public MenuFragmentTest() {
super(FragmentAdapter.class);
}
public void testMenuOptions() {
System.out.println(getActivity().findViewById(1));
}
}
我正在尝试用来测试应用中片段的广义FragmentAdapter:
public class FragmentAdapter extends FragmentActivity {
static Fragment fragmentUnderTest;
public static void setFragmentUnderTest(Fragment fragmentUnderTest) {
FragmentAdapter.fragmentUnderTest = fragmentUnderTest;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(ll);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(fragmentUnderTest, null);
fragmentTransaction.commit();
}
}
主应用程序安装成功后,Android JUnit Test Runner失败并显示
java.lang.RuntimeException: Could not find test class. Class: com.XXX.core.MenuFragment
为什么Test Runner找不到我正在运行的测试类?
答案 0 :(得分:2)
显然,测试包的名称确实很重要:
来自http://developer.android.com/tools/testing/testing_android.html:
如果被测应用程序的软件包名称为com.mydomain.myapp,则Android工具会将测试软件包名称设置为com.mydomain.myapp.test
更改包检测targetPackage属性不足以允许您在与测试中的活动相同的包中创建测试,即使测试位于单独的测试项目中也是如此。