【问题标题】:Android : How to make sure that sharedPreference get clear before running a large nos of test in Instrumentation testingAndroid : 如何在 Instrumentation 测试中运行大量测试之前确保 sharedPreference 变得清晰
【发布时间】:2019-08-26 08:34:54
【问题描述】:

我有一个包含登录页面的应用程序。在此登录页面中,如果用户已经登录,我将用户凭据存储在共享首选项中,它将直接进入启动屏幕,然后进入仪表板。但在 android Instrumentation 测试的情况下。我的测试用例只运行一次,因为用户凭据第一次没有存储在共享首选项中。一旦凭据存储在共享首选项中,它将在测试开始时自动将测试用例重定向到启动屏幕。因此我的测试用例失败了。

我想在每次运行登录测试用例之前清除我的应用共享偏好。

我尝试使用 getInstrumentation().getContext() 从获取上下文中清除我的共享首选项。但它似乎不起作用。

我必须编写多个测试用例,我必须在同一个类中运行至少 15 个测试用例。所以对于每个@test,我都需要首先清除共享偏好。

下面是我的登录测试用例代码:

import android.app.Instrumentation;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Button;
import android.widget.EditText;

import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import salesken.app.R;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)

public class LoginActivityTest {

    @Rule
    public ActivityTestRule < LoginActivity > loginActivityTestRule = new ActivityTestRule < LoginActivity > (LoginActivity.class);

    private LoginActivity loginActivity = null;

    private String email_input = "dummy@user.com";
    private String password_input = "password1";
    private int timeout = 5000;
    private SharedPreferences sharedPreferences;

    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(SplashScreenActivity.class.getName(), null, false);

    @Before
    public void setUp() throws Exception {
        loginActivity = loginActivityTestRule.getActivity();
        clearsharedPref();

    }

    @After
    public void tearDown() throws Exception {
        loginActivity = null;
    }

    @Test
    public void autenticationcheck() {
        clearsharedPref();

        EditText email = loginActivity.findViewById(R.id.email);
        EditText password = loginActivity.findViewById(R.id.password);
        Button login = loginActivity.findViewById(R.id.login);
        email.setText(email_input);
        password.setText(password_input);
        onView(withId(R.id.login)).perform(ViewActions.click());
        SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
        assertNotNull(splashScreenActivity);
        splashScreenActivity.finish();
    }

    @Test
    public void emptyEmail() {
        clearsharedPref();
        EditText email = loginActivity.findViewById(R.id.email);
        EditText password = loginActivity.findViewById(R.id.password);
        Button login = loginActivity.findViewById(R.id.login);
        email.setText("");
        password.setText(password_input);
        onView(withId(R.id.login)).perform(ViewActions.click());
        SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
        assertNull(splashScreenActivity);
    }

    @Test
    public void emptyPassword() {
        clearsharedPref();
        EditText email = loginActivity.findViewById(R.id.email);
        EditText password = loginActivity.findViewById(R.id.password);
        Button login = loginActivity.findViewById(R.id.login);
        email.setText("");
        password.setText(password_input);
        onView(withId(R.id.login)).perform(ViewActions.click());
        SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
        assertNull(splashScreenActivity);
    }

    public void clearsharedPref() {
        Context context = getInstrumentation().getTargetContext().getApplicationContext();
        sharedPreferences = context.getSharedPreferences("SaleskenProComplexKey", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }

}

【问题讨论】:

    标签: android android-testing android-instrumentation


    【解决方案1】:

    你可以全部清除

    Context context = getInstrumentation().getContext();
    SharedPreferences preferences = context.getSharedPreferences("mysharedpref", 
    Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.clear(); 
    editor.commit();
    

    【讨论】:

    • 这段代码永远不会在android单元测试用例上运行
    • 检查有问题的更新代码问题是我有三个测试用例autenticationcheck、emptyEmail、emptyPassword。您提到的代码适用于 autenticationcheck 和 emptyEmail。但是当它运行空密码时,它将无法清除共享首选项。我在模拟器像素 api 28 上运行我的测试用例
    • 它工作了几次。但我认为当您像 15-20 一样扩展测试用例时,在测试之间一次又一次地清除共享偏好存在一些问题
    • SharedPreferences 并非每次都能在某些设备上完美运行,尤其是小米设备。您需要重新启动设备才能使其发生。模拟器也有一些错误@FerozSiddiqui
    • 我正在写一个回归套件。这让我很困扰,因为它会给我的回归套件造成混乱。在执行下一个测试用例之前我应该​​先睡一会儿吗?
    【解决方案2】:

    清空SharedPrferences应该够了,如果不行,你可以删除它而不是清空:

    String filePath = getApplicationContext().getFilesDir().getParent()+"/shared_prefs/prefs_name.xml";
        File deletePrefFile = new File(filePath);
         deletePrefFile.delete();
    

    如果我无法通过简单的方式获取应用程序的上下文:

    public class ApplicationContext extends Application {
    
        private static ApplicationContext INSTANCE;
    
        @Override
        public void onCreate() {
            super.onCreate();
            INSTANCE = this;
        }
    
        public static ApplicationContext getInstance() {
            return INSTANCE;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多