java - 从单独的类运行单独的 JUnit 测试

标签 java unit-testing junit

我正在尝试从一个单独的类运行测试,可以在其中编译和报告信息。然而,我在运行单独的测试时遇到了困难。

我尝试过:

for (int i = 0; i < testRuns; i++) {
  JUnitCore.runClasses(InternetExplorerTestClass.class, MozillaFirefoxTestClass.class, GoogleChromeTestClass.class);
}

但这限制了我对结果和报告数据的控制。

如何从测试套件运行单个测试?预先感谢您。

最佳答案

看起来你正在做类似 Selenium 测试的事情?如果您使用 Gradle 作为构建工具,您可以通过使用“include”过滤器选项轻松运行一项特定测试,如下所示。 (您也可以使用 Ant、SBT 或 Maven 做类似的事情)。就我个人而言,我认为使用构建工具来选择要运行的测试比编写代码来运行某些类更优雅。

tasks.withType(Test) {

    jvmArgs '-Xms128m', '-Xmx1024m', '-XX:MaxPermSize=128m'
    maxParallelForks = 4

    // System properties passed to tests (if not http://localhost:8001/index.html)
    systemProperties['testProtocol'] = 'http'
    systemProperties['testDomain'] = 'djangofan.github.io'
    systemProperties['testPort'] = 80
    systemProperties['testUri'] = '/html-test-site/site'
    systemProperties['hubUrl'] = 'localhost'
    systemProperties['hubPort'] = '4444'

}

task runParallelTestsInFirefox(type: Test) {
    description = 'Runs all JUnit test classes in parallel threads.'
    include '**/TestHandleCache*.class'
    testReportDir = file("${reporting.baseDir}/ParallelTestsFF")
    testResultsDir = file("${buildDir}/test-results/ParallelTestsFF")

    // System properties passed to tests
    systemProperties['browserType'] = 'firefox'

    // initial browser size and position
    systemProperties['windowXPosition'] = '100'
    systemProperties['windowYPosition'] = '40'
    systemProperties['windowWidth'] = '400'
    systemProperties['windowHeight'] = '600'    
}

This is taken from a example project I wrote here .

关于java - 从单独的类运行单独的 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17156902/

相关文章:

angular - 单击事件发生时如何对下拉菜单进行单元测试

wcf - 单元测试 LinqToSqlDomainService

java - 如何获取 JUnit 5 中测试的当前重复次数?

java - 如何编写验证测试?

java - Android Firebase - 卡未在 RecyclerView 和 NullPointerException 中显示

java - 主应用程序的软件集成和共享数据库

AngularJS 测试?单元测试和端到端测试?

java - Play Framework [2.3.0 - Java] 在测试期间访问 play.Configuration.root()

Java - MongoDB 不区分大小写,不检查完全匹配

java - Android Speech to text API 从 onActivityResult 方法中获取变量值