简体   繁体   English

在旧版Android Studio项目中创建测试文件夹

[英]Creating test folders in legacy Android Studio project

I've been having issues getting test folders adding to my existing legacy project. 我在将测试文件夹添加到我现有的旧项目中时遇到了问题。 I've been looking through Stack and whilst these users are all having similar issues, the solutions haven't yet worked. 我一直在浏览Stack,尽管这些用户都有类似的问题,但解决方案尚未奏效。 I'm new at working with gradle, so this has been a bit confusing. 我是使用gradle的新手,所以这有点令人困惑。

How to add test folders to an older Android Studio Project 如何将测试文件夹添加到较旧的Android Studio项目

Android studio doesn't recognise source folders Android Studio无法识别源文件夹

Cannot Create Testing Folders In Android Studio 无法在Android Studio中创建测试文件夹

The problem can be seen below: 该问题可以在下面看到:

When looking at android view, I get this. 当查看android view时,我明白了。 You can see that there's a visible alpha and main folder. 您会看到有一个可见的Alpha和主文件夹。

Looking at project view , you can see the addition of the src/test/java and src/androidTest/java to the folder structure, but I can't get them to register in Android view. 查看项目视图 ,您可以看到在文件夹结构中添加了src/test/javasrc/androidTest/java ,但是我无法让它们在Android视图中注册。

I've tried setting the sourceSets in the gradle file: 我试过在gradle文件中设置sourceSets:

sourceSets {
    beta {
        java.srcDirs = [ 'src/general/java']
        res.srcDirs=['src/general/res']
    }
    general {
        java.srcDirs = [ 'src/general/java', 'src/androidTest/java/', 'src/test/java']
        res.srcDirs=['src/general/res']
    }
    demo {
        java.srcDirs = [ 'src/alpha/java']
        res.srcDirs=['src/alpha/res']
    }
    alpha {
        java.srcDirs = [ 'src/alpha/java', 'src/androidTest/java/', 'src/test/java']
        res.srcDirs=['src/alpha/res']
    }

    test {
        java.srcDirs = ['src/test/java']
    }

    androidTest {
        java.srcDirs = ['src/androidTest/java']
    }

}

where test and androidTest are new. test和androidTest是新的。 (Note: the others all have buildflavors associated with them, and I haven't created any for the tests. Could this be the issue?) (注意:其他都具有与之关联的buildflavor,而我还没有为测试创建任何香料。这可能是问题吗?)

Some of the previous suggestions have said it's to do with build version and that there should be a testing related dropdown - here . 先前的一些建议已经表明,这与构建版本有关,并且应该有一个与测试相关的下拉列表- 在此处 I've tested all of the builds with no success. 我已经测试了所有构建,但均未成功。

Another user suggested using the build task assembleAndroidTest to generate the folders, which gives the following error: 另一个用户建议使用构建任务assembleAndroidTest生成文件夹,这将产生以下错误:

FAILURE: Build failed with an exception. 失败:构建失败,发生异常。

  • What went wrong: Execution failed for task ':library:processDebugAndroidTestResources'. 出了什么问题:任务':library:processDebugAndroidTestResources'的执行失败。

    Android resource linking failed Android资源链接失败

I've been having other issues with trying to get testing working whilst following through google's recommended documentation. 在尝试通过Google推荐的文档进行测试的同时,我遇到了其他问题。 Dependencies and manifest declarations have worked fine but trying to add 依赖项和清单声明工作正常,但尝试添加

useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'

I get 我懂了

Error:Unable to find optional library: android.test.runner 错误:找不到可选库:android.test.runner

Which I've seen referenced for apache legacy but little for this situation. 我见过关于Apache遗产的参考资料,但在这种情况下很少提及。 I think this is probably a different problem but included it in case it was relevant. 我认为这可能是一个不同的问题,但是如果涉及到它就包括在内。

Android studio is at version 3.2.1. Android Studio的版本为3.2.1。

So in summary: 因此,总而言之:

How do I add test folders to the project and get them to display in Android view? 如何将测试文件夹添加到项目并使其显示在Android视图中?

Thanks. 谢谢。

I found the problem. 我发现了问题。 A simple fix. 一个简单的修复。

Inside the main, the folder structure was 在主体内部,文件夹结构为

 main/java/com/windowmaker/measure 

and the folder structure for my test folders was 我的测试文件夹的文件夹结构是

 test/java 

By changing the path to 通过更改路径

test/java/com/windowmaker/measure

it now displays correctly. 现在可以正确显示。

Hooray! 万岁!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM