Maven没有从测试套件类中找到测试

时间:2017-09-18 18:43:29

标签: java maven unit-testing testing junit

我正在使用maven来运行一系列JUnit测试,我已经更改了代码,因此我不使用@Test注释而是使用这样的测试套件类:

import os
import urllib

from bs4 import BeautifulSoup
# Python 3.x
from urllib.request import urlopen, urlretrieve
from urllib.error import HTTPError

URL = 'https://www.rbi.org.in/Scripts/bs_viewcontent.aspx?Id=2009'
OUTPUT_DIR = ''  # path to output folder, '.' or '' uses current folder

u = urlopen(URL)
try:
    html = u.read().decode('utf-8')
finally:
    u.close()

soup = BeautifulSoup(html, "html.parser")
for link in soup.select('a[href^="http://"]'):
    href = link.get('href')
if not any(href.endswith(x) for x in ['.csv','.xls','.xlsx']):
    continue

filename = os.path.join(OUTPUT_DIR, href.rsplit('/', 1)[-1])

# We need a https:// URL for this site
href = href.replace('http://','https://')

try:
    print("Downloading %s to %s..." % (href, filename) )
    urlretrieve(href, filename)
    print("Done.")
except urllib.error.HTTPError as err:
    if err.code == 404:
        continue

当我跑

public class MyTests {
  public static Test suite() {
    TestSuite ts = new TestSuite("My Test Suite");
    ts.addTest(new CustomTestCase1("Test 1"));
    ts.addTest(new CustomTestCase2("Test 2"));
    ...
    return ts;
  }
}

我得到没有执行任何测试!

有人能指出我如何使用maven运行测试而不是为每个类创建测试运行器吗?

2 个答案:

答案 0 :(得分:1)

您应该能够使用-DrunSuite标志运行套件:

mvn clean -U -fn -DrunSuite=MyTests test 

答案 1 :(得分:0)

根据@Mureinik的建议,您可以使用runSuite param执行命令。

mvn clean test -U -fn -DrunSuite=**/MyTests.class

只需使用命令mvn test即可作为默认测试运行。您也可以在surefire插件配置中包含该类:

<properties>
    <suite>**/MyTests.class</suite>
</properties>
....
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
        <includes>
            <include>${suite}</include>
        </includes>
    </configuration>
</plugin>