如何将数据库测试装置从单元测试应用程序传输到设备

发布于 2024-09-24 17:04:22 字数 305 浏览 1 评论 0原文

我正在编写一个 Android JUnit 测试,想要复制/重置一个测试夹具文件(它是一个 SQLite 数据库文件)。如果我在主应用程序中,我知道我可以将该文件放在资产目录中并使用 getResources().getAssets().open(sourceFile)

但是,此 API 似乎在 ActivityInstrumentationTestCase2 类中不可用。

有没有一种简单的方法可以从测试 PC 复制文件,或者我应该在设备上保留测试夹具的新副本并将其复制到临时文件上?

谢谢!

I'm writing an Android JUnit test and want to copy/reset a test fixture file (it's an SQLite database file.) If I were within the main application, I know I could just place the file in the assets directory and use getResources().getAssets().open(sourceFile)

However, this API appears to be unavailable from the ActivityInstrumentationTestCase2 class.

Is there an easy way to copy a file over from the testing PC, or should I just keep a fresh copy of a test fixture on the device and copy it over a temporary file?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海拔太高太耀眼 2024-10-01 17:04:22

测试应用程序和主应用程序中的资源可以在仪器测试用例中单独访问。如果要访问测试项目本身的 res/raw 或 assets 文件夹中的资源,可以使用

getInstrumentation().getContext().getResources()

要访问正在测试的应用程序(“目标”应用程序)中的资源,请使用

getInstrumentation().getTargetContext().getResources()

注意,但是,您永远不能修改assets文件夹中的文件;

getResources().getAssets().open(sourceFile)

返回一个输入流。没有办法修改该文件。这是因为资产被压缩存储在 APK 内,并且根本不可写。

如果您想要做的是修改您正在测试的 Activity 使用的文件的路径,您应该使用 ActivityUnitTestCasesetActivityContext()RenamingDelegatingContext。这允许您通过指定目录前缀将上下文中的文件和数据库访问重定向到新目录。您甚至可以使用 更复杂的构造函数来包装大多数操作的目标上下文,但使用测试应用程序的上下文进行文件操作,因此该活动将访问存储在测试应用程序而不是主应用程序中的文件,但仍使用主应用程序中的其他资源。

The resources in your test application and your main application are accessible separately in an instrumentation test case. If you want to access resources that are in the res/raw or assets folder of the test project itself, you can use

getInstrumentation().getContext().getResources()

To access resources in the application being tested (the "target" application), use

getInstrumentation().getTargetContext().getResources()

Note, however, that you can never modify files in the assets folder;

getResources().getAssets().open(sourceFile)

returns an InputStream. There's no way to modify the file. That's because assets are stored compressed inside the APK, and are not really writeable at all.

If what you want to do is modify the path to the files the Activity you're testing uses, you should use ActivityUnitTestCase and setActivityContext() with a RenamingDelegatingContext. This allows you to redirect file and database access in a context to a new directory by specifying a directory prefix. You can even use the more complex constructor to wrap the target context for most operations, but use your test application's context for file operations, so the activity will access files stored in the test application rather than the primary application but still use other resources in the primary application.

安静被遗忘 2024-10-01 17:04:22

为了实现这一点,我所做的(以一种不太优雅的方式)是将测试夹具复制到我的设备(或模拟设备)上。我将其命名为“cleantestdatabase.db”。然后在测试代码中,我将其复制到“testdatabase.db”,以便我可以通过测试对其进行修改,但将其重置为已知状态。这是代码:

copyFile("cleantestdatabase.db", "testdatabase.db");

private void copyFile(String source, String dest) throws IOException{
    String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + getActivity().getString(R.string.default_dir);
    File newDir = new File(rootPath);
    boolean result = newDir.mkdir();
    if(result == false){
        Log.e("Error", "result false");
    }

    InputStream in = new FileInputStream(rootPath + source);    
    File outFile = new File(rootPath + dest);
    if(outFile.exists()) {
        outFile.delete();
    }
    outFile.createNewFile();

    OutputStream out = new FileOutputStream(outFile);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

What I've done to accomplish this (in a not very elegant way) is to copy the test fixture onto my device (or emulated device.) I named it "cleantestdatabase.db." Then in the test code, I copy it to "testdatabase.db," so that I can modify it with my tests but reset it to a known state. Here's the code:

copyFile("cleantestdatabase.db", "testdatabase.db");

private void copyFile(String source, String dest) throws IOException{
    String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + getActivity().getString(R.string.default_dir);
    File newDir = new File(rootPath);
    boolean result = newDir.mkdir();
    if(result == false){
        Log.e("Error", "result false");
    }

    InputStream in = new FileInputStream(rootPath + source);    
    File outFile = new File(rootPath + dest);
    if(outFile.exists()) {
        outFile.delete();
    }
    outFile.createNewFile();

    OutputStream out = new FileOutputStream(outFile);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文