2012-03-07 34 views
14

Tôi đang cố di chuyển từ Ant build sang Gradle trong dự án của mình. Có một loạt các trường hợp thử nghiệm (các lớp con của junit.framework.TestCase) và một vài bộ kiểm thử (các lớp con của junit.framework.TestSuite). Gradle tự động chọn tất cả các trường hợp thử nghiệm (các lớp con của junit.framework.TestCase) để chạy, nhưng không phải là các dãy (lớp con của junit.framework.TestSuite).Làm thế nào để chạy Junit TestSuites từ gradle?

Tôi có thể làm việc xung quanh bằng cách gọi ant.junit để chạy nó. Nhưng, tôi cảm thấy cần phải có một cách dễ dàng để buộc họ phải chọn và chạy. Tôi không thể tìm thấy bất cứ điều gì trong tài liệu. Tui bỏ lỡ điều gì vậy?

+1

Bạn dường như mâu thuẫn với chính mình ... bạn đang nói rằng 'Gradle tự động nhặt tất cả các trường hợp thử nghiệm để chạy.' Vì vậy, vấn đề là nó biên dịch chúng, nhưng không chạy chúng? Vui lòng làm rõ. –

+0

@c_maker: Tôi đã chỉnh sửa văn bản để cải thiện độ rõ nét. Cảm ơn bạn đã chỉ ra. – James

Trả lời

13

Đây là khó khăn cho tôi để tìm ra, nhưng đây là một ví dụ:

// excerpt from https://github.com/djangofan/WebDriverHandlingMultipleWindows 
package webdriver.test; 
import http.server.SiteServer; 
import java.io.File; 
import java.io.IOException; 
import org.junit.AfterClass; 
import org.junit.BeforeClass; 
import org.junit.runner.RunWith; 
import org.junit.runners.Suite; 

@RunWith(Suite.class) 
@Suite.SuiteClasses({ TestHandleCacheOne.class, TestHandleCacheThree.class, TestHandleCacheThree.class }) 
public class SuiteOne extends MultiWindowUtils { 

    public static SiteServer fs; 

    @BeforeClass 
    public static void setUpSuiteOne() { 
     File httpRoot = new File("build/resources/test"); 
     System.out.println("Server root directory is: " + httpRoot.getAbsolutePath()); 
     int httpPort = Integer.parseInt("8080"); 
     try { 
      fs = new SiteServer(httpPort , httpRoot); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     initializeBrowser("firefox"); 
     System.out.println("Finished setUpSuiteOne"); 
    } 

    @AfterClass 
    public static void tearDownSuiteOne() { 
     closeAllBrowserWindows(); 
     System.out.println("Finished tearDownSuiteOne"); 
    } 

} 

Và một build.gradle tương tự như sau:

apply plugin: 'java' 
apply plugin: 'eclipse' 
group = 'test.multiwindow' 

ext { 
    projTitle = 'Test MultiWindow' 
    projVersion = '1.0' 
} 

repositories { 
    mavenCentral() 
} 

dependencies { 
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+' 
    compile group: 'junit', name: 'junit', version: '4.+' 
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+' 
} 

task testGroupOne(type: Test) { 
    //include '**/*SuiteOne.*' 
    include '**/SuiteOne.class' 
    reports.junitXml.destination = "$buildDir/test-results/SuiteOne") 
    reports.html.destination = "$buildDir/test-results/SuiteOne") 
} 

task testGroupTwo(type: Test) { 
    //include '**/*SuiteTwo.*' 
    include '**/SuiteTwo.class' 
    reports.junitXml.destination = "$buildDir/test-results/SuiteTwo") 
    reports.html.destination = "$buildDir/test-results/SuiteTwo") 
} 
+0

Tôi nên nói rằng kể từ khi tôi đăng câu trả lời này DSL cho cấu hình kiểm tra đơn vị đã thay đổi một chút. Cẩn thận. – djangofan

Các vấn đề liên quan