AndroidTestCase Solo 应用

TestCase在项目中是很重要,但仍然有很多人不知道怎么去写,今天我将我写的一段Case给大家分享一下。正常我们都会继承AndroidTestCase,我这里继承的ApplicationTestCase. 方法命名最好Test什么方法就在该方法前test
public class InteractiveIssueDataHandlerTest extends ApplicationTestCase<App> {
	public InteractiveIssueDataHandlerTest() {
		super(App.class);
	}
//add some parameter if you need.
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		createApplication();
		SharedPreferences prefs = this.getContext().getSharedPreferences("settings",
				Context.MODE_PRIVATE);
		String sInstallationUuid = prefs.getString("installationUuid", null);
		if (sInstallationUuid == null) {
			sInstallationUuid = UUID.randomUUID().toString();

			Editor editor = prefs.edit();
			editor.putString("installationUuid", sInstallationUuid);
			editor.commit();
		}
	}
/**
	 * Tests the parsing of the issue data xml response.
	 * @throws ServiceException
	 */
	public void testSingleIssue() throws ServiceException {
		IssueData issueData = getIssueData();
		
		assertEquals(packageSize, issueData.getPackageSize());
		assertTrue(issueData.isAutoStart());
assertNotNull(issueData.getHostName());
			}
/**
	 * return IssueData for other method
	 */
	 IssueData getIssueData(){
		
		IssueDataHandler handler = new IssueDataHandler();
		handleXmlFilePath(handler, ISSUE_DATA_XML_PATH, false);
		IssueData issueData = handler.getIssueData(); 
		System.out.println("----issueData--" + issueData);
		return issueData;
	}

Solo 我用的不多,但是确很方便,给大家一部分代码供参考
/**
 * A class to test the MoreActivity class.
 * 
 * @author Georgi
 * 
 */

public class MoreActivityTest extends ZinioActivityTransitionTest<MoreActivity> {

	private Solo solo;
	private MoreActivity mMoreActivity;
	private TextView mLegalNotices;
	private TextView mSignOut;
	private TextView mManageTips;
	private Instrumentation mInstrumentation;

	/*
	 * Constructor
	 */
	public MoreActivityTest() {
		super(MoreActivity.class);
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();

		try {
			
			UserManager.get().logInSync(new Handler(), 
					TestUtil.getExistingUserId(), TestUtil.getExistingUserPassword());
			
		} catch (Exception e) {
			fail();
		}
		
		solo = new Solo(getInstrumentation(), getActivity());

		mMoreActivity = this.getActivity();

		mLegalNotices = (TextView) this.getActivity().findViewById(R.id.more_legal_notices);
		mSignOut = (TextView) this.getActivity().findViewById(R.id.more_sign_out);
		mManageTips = (TextView) this.getActivity().findViewById(R.id.more_manage_tips);
		mInstrumentation = getInstrumentation();

	}

	@Override
	public void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Tests whether the 6 exact TextViews on that page are drawn when opening
	 * this Activity
	 */
	public void testMenuMembersInitialization() {
		
		ModuleRegistry.get();
		
		int[] pageTextViewsIds = new int[] { 
				R.id.more_change_password,
				R.id.more_faq,
				R.id.more_legal_notices,
				R.id.more_manage_tips,
				R.id.more_sign_out
		};

		List<TextView> textViewsOnPage = findAllViews(pageTextViewsIds, TextView.class);

		TextView v = (TextView) mMoreActivity.findViewById(R.id.more_change_password);
		
		ViewGroup vp = (ViewGroup) v.getParent();

		List<TextView> viewsList = new ArrayList<TextView>();
		
		for (int i = 0; i < vp.getChildCount(); i++) {
			
			if (vp.getChildAt(i).getClass() == TextView.class) {
				viewsList.add((TextView) vp.getChildAt(i));
				Log.i("added", Integer.toString(i));
			}
		}

		if (!viewsList.containsAll(textViewsOnPage)) {
			fail("One or more of the main view components are not initialized");
		}

	}

	/**
	 * Tests whether the More Activity is initialized
	 */
	public void testActivityElementsExistence() {
		
		assertNotNull(mMoreActivity);
		assertNotNull(mLegalNotices);
		assertNotNull(mSignOut);
		assertNotNull(mManageTips);
		assertNotNull(mInstrumentation);
	}

	/**
	 * This tests clicks on 'managed tips' and then clicks on back button
	 */
	public void testManageTips() {
		
		solo.clickOnText(mMoreActivity.getString(R.string.manage_tips));

		assertTrue(solo.searchText(mMoreActivity.getString(R.string.dialog_manage_tips_title)));

		solo.clickOnText(mMoreActivity.getString(R.string.cancel));
	}

	/**
	 * This tests clicks on 'managed tips' and then clicks on back button
	 */
	public void testSignOutCancel() {
	
		solo.clickOnText(mMoreActivity.getString(R.string.sign_out));

		assertTrue(solo.searchText(mMoreActivity.getString(R.string.dialog_sign_out_title)));

		solo.clickOnText(mMoreActivity.getString(R.string.cancel));
	}

	/**
	 * Test whether the "change password" link starts the proper Activity
	 */
	public void testChangePasswordIntent() {
	    
		solo.clickOnText(mMoreActivity.getString(R.string.change_password));

		solo.sleep(10000);
		solo.assertCurrentActivity("Expected ChangePasswordActivity", ChangePasswordActivity.class);
	}

	/**
	 * Test whether the version code is drawn and displayed and is not null
	 */
	public void testVersionCodeDisplayTest() {
		
		TextView v = findView(R.id.more_vc, TextView.class);
		assertTrue(v != null);
		String vcText = v.getText().toString();
		assertTrue(vcText.contains(UserManager.get().getCurrentUser().getUsername()));
	}
}

你可能感兴趣的:(xml,vc++)