TestNG 跳过测试 - 为什么?

发布于 2024-10-20 02:51:08 字数 7743 浏览 2 评论 0原文

我正在使用 testng 和 selenium 测试一个网络应用程序。测试主要包括打开应用程序的几个页面,并针对每个页面执行一些特定的活动。因此,我有一个执行“打开页面”测试的抽象基类,并定义了一个用作该测试的数据提供程序的抽象方法。然后有几个扩展类为数据提供者提供实现,并且除了基类之外还有几个不同的测试。我有一个 testsuite.xml,其中包含所有类,这是我从 eclipse 运行的。

问题是,当我启动测试执行时,testng 在所有对象的基类中运行测试,但系统地跳过扩展类中的所有其他测试。有谁知道为什么?任何信息都将非常感激...

为了完成信息,这里是我使用的一些类和 xml:

基类:

public abstract class BaseWebAppPageTest {

    @Test(dataProvider="getMenuLink")
    public void testOpen(String menulink){
        GenericPageActions.openPage(TestingContext.getSelenium(), menulink);
    }

    protected abstract String[][] getMenuLink();

}

和一些扩展类:

TestLanguages:

public class TestLanguages extends BaseWebAppPageTest{

    @Test(dependsOnMethods={"testOpen"}, dataProvider="getCreateData")
    public void testCreateCorrect(String code, String description, String textDirection, String flag){
        Selenium selenium = TestingContext.getSelenium();
        LanguagesManagementActions.create(selenium, code, description, textDirection, flag);
    Assert.assertTrue(selenium.isTextPresent("Successfully created language"));
    }

    @Test(dependsOnMethods={"testCreateCorrect"}, dataProvider="getCreateData")
    public void testFilter(String code, String description, String textDirection, String flag){
        Selenium selenium = TestingContext.getSelenium();
        LanguagesManagementActions.filterTable(selenium, 2, code, 30000);
    Assert.assertTrue(selenium.getXpathCount("xpath=//span[.='"+code+"']").intValue() == 1);
    }

    @Test(dependsOnMethods={"testCreateCorrect"}, dataProvider="getCreateData")
    public void testModify(String code, String description, String textDirection, String flag){
        Selenium selenium = TestingContext.getSelenium();
        LanguagesManagementActions.modify(TestingContext.getSelenium(), code, description, textDirection, flag);
        Assert.assertTrue(selenium.isTextPresent("Successfully updated language"));
    }

    @Override
    @DataProvider
    protected String[][] getMenuLink() {
        return(TestingContext.getDataReader().getTableArray("openviewpage", "MULTILINGUAL_LANGUAGES"));
    }

    @DataProvider
    protected String[][] getCreateData() {
        return(TestingContext.getDataReader().getTableArray("multilingualcreate", "LANGUAGES"));
    }

}

TestTranslations:

public class TestTranslations extends BaseWebAppPageTest{

@Test(dependsOnMethods={"testOpen"}, dataProvider="getCreateData")
public void createCorrect(String code, String targetLanguage, String translation){
    Selenium selenium = TestingContext.getSelenium();
    TranslationsManagementActions.create(selenium, code, targetLanguage, translation);
    Assert.assertTrue(selenium.isTextPresent("Successfully created translation"));
}

@Test(dependsOnMethods={"createCorrect"}, dataProvider="getCreateData")
public void update(String code, String targetLanguage, String translation){
    Selenium selenium = TestingContext.getSelenium();
    TranslationsManagementActions.update(selenium, code, targetLanguage, translation);
    Assert.assertTrue(selenium.isTextPresent("Successfully updated translation"));
}

@Test(dependsOnMethods={"createCorrect"}, dataProvider="getCreateData")
public void filter(String code, String targetLanguage, String translation){
    Selenium selenium = TestingContext.getSelenium();
    TranslationsManagementActions.filterTable(selenium, 2, code, 30000);
    Assert.assertTrue(selenium.isElementPresent("xpath=//span[.='"+translation+"']"));
}

@Override
@DataProvider
protected String[][] getMenuLink() {
    return(TestingContext.getDataReader().getTableArray("openviewpage", "MULTILINGUAL_TRANSLATIONS"));
}

@DataProvider
protected String[][] getCreateData() {
    return(TestingContext.getDataReader().getTableArray("multilingualcreate", "TRANSLATIONS"));
}

}

最后,TestSuite.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
    <suite name="WebAppSuiteTest" parallel="none">
      <parameter name="selenium.host" value="localhost" />
      <parameter name="selenium.port" value="5555" />
      <parameter name="selenium.browser" value="*firefox3 C:\\Documents and Settings\\dgarcia\\Local Settings\\Application Data\\Mozilla Firefox\\firefox.exe" />
      <parameter name="selenium.url" value="http://localhost:8080/standard-webapp-war/home.seam" />
      <parameter name="selenium.timeout" value="1000000" />
      <parameter name="test.data.filepath" value="src\\test\\resources\\datatest_.xls" />

  <test name="standard" preserve-order="true" >
    <classes>
        <class name="com.standard.webapp.common.TestingContext" />      
        <class name="com.standard.webapp.login.TestLogin"/>
        <class name="com.standard.webapp.TestLanguages"/>
        <class name="com.standard.webapp.TestTranslations"/>
        </class>
    </classes>
  </test>
</suite>

没有例外,也没有输出中任何跳过这些测试的原因。我不太确定您提到的输出,因此我将在此处粘贴生成的“myWebAppTest.xml”的内容以及测试结果:

<testsuite hostname="SP2L0044" name="com.sicpa.standard.dms.webapp.selenium.common.BaseWebAppPageTest" tests="14" failures="0" timestamp="4 Mar 2011 08:45:57 GMT" time="27.141" errors="0">
  <testcase name="testLoginLoadHome" time="2.188" classname="com.sicpa.standard.dms.webapp.selenium.login.TestLogin"/>
  <testcase name="testCreateCorrect" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testFilter" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testUpdate" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testView" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testCreateCorrect" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages">
    <skipped/>
  </testcase>
  <testcase name="testFilter" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages">
    <skipped/>
  </testcase>
  <testcase name="testModify" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages">
    <skipped/>
  </testcase>
  <testcase name="createCorrect" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations">
    <skipped/>
  </testcase>
  <testcase name="filter" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations">
    <skipped/>
  </testcase>
  <testcase name="update" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations">
    <skipped/>
  </testcase>
  <testcase name="testOpen" time="2.297" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages"/>
  <testcase name="testOpen" time="12.61" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes"/>
  <testcase name="testOpen" time="9.469" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations"/>
</testsuite>

I am testing a web app with testng and selenium. The tests mainly consist in opening several pages of the app, and do some activities specific for each page. So I have an abstract base class which performs the "open page" test, and that defines an abstract method which is used as data provider for that test. Then there are several extending classes which provide implementation for the data provider, and which have several different tests apart from that of the base class. I have a testsuite.xml where all the classes are included, which is what I run from my eclipse.

The problem is that when I launch the test execution, testng runs the test in the base class for all objects, but skips systematically every other tests in the extending classes. Does anyone know why? Any information will be very appreciated...

To complete information, here are some of the classes and the xml I use:

Base class:

public abstract class BaseWebAppPageTest {

    @Test(dataProvider="getMenuLink")
    public void testOpen(String menulink){
        GenericPageActions.openPage(TestingContext.getSelenium(), menulink);
    }

    protected abstract String[][] getMenuLink();

}

And some extending classes:

TestLanguages:

public class TestLanguages extends BaseWebAppPageTest{

    @Test(dependsOnMethods={"testOpen"}, dataProvider="getCreateData")
    public void testCreateCorrect(String code, String description, String textDirection, String flag){
        Selenium selenium = TestingContext.getSelenium();
        LanguagesManagementActions.create(selenium, code, description, textDirection, flag);
    Assert.assertTrue(selenium.isTextPresent("Successfully created language"));
    }

    @Test(dependsOnMethods={"testCreateCorrect"}, dataProvider="getCreateData")
    public void testFilter(String code, String description, String textDirection, String flag){
        Selenium selenium = TestingContext.getSelenium();
        LanguagesManagementActions.filterTable(selenium, 2, code, 30000);
    Assert.assertTrue(selenium.getXpathCount("xpath=//span[.='"+code+"']").intValue() == 1);
    }

    @Test(dependsOnMethods={"testCreateCorrect"}, dataProvider="getCreateData")
    public void testModify(String code, String description, String textDirection, String flag){
        Selenium selenium = TestingContext.getSelenium();
        LanguagesManagementActions.modify(TestingContext.getSelenium(), code, description, textDirection, flag);
        Assert.assertTrue(selenium.isTextPresent("Successfully updated language"));
    }

    @Override
    @DataProvider
    protected String[][] getMenuLink() {
        return(TestingContext.getDataReader().getTableArray("openviewpage", "MULTILINGUAL_LANGUAGES"));
    }

    @DataProvider
    protected String[][] getCreateData() {
        return(TestingContext.getDataReader().getTableArray("multilingualcreate", "LANGUAGES"));
    }

}

TestTranslations:

public class TestTranslations extends BaseWebAppPageTest{

@Test(dependsOnMethods={"testOpen"}, dataProvider="getCreateData")
public void createCorrect(String code, String targetLanguage, String translation){
    Selenium selenium = TestingContext.getSelenium();
    TranslationsManagementActions.create(selenium, code, targetLanguage, translation);
    Assert.assertTrue(selenium.isTextPresent("Successfully created translation"));
}

@Test(dependsOnMethods={"createCorrect"}, dataProvider="getCreateData")
public void update(String code, String targetLanguage, String translation){
    Selenium selenium = TestingContext.getSelenium();
    TranslationsManagementActions.update(selenium, code, targetLanguage, translation);
    Assert.assertTrue(selenium.isTextPresent("Successfully updated translation"));
}

@Test(dependsOnMethods={"createCorrect"}, dataProvider="getCreateData")
public void filter(String code, String targetLanguage, String translation){
    Selenium selenium = TestingContext.getSelenium();
    TranslationsManagementActions.filterTable(selenium, 2, code, 30000);
    Assert.assertTrue(selenium.isElementPresent("xpath=//span[.='"+translation+"']"));
}

@Override
@DataProvider
protected String[][] getMenuLink() {
    return(TestingContext.getDataReader().getTableArray("openviewpage", "MULTILINGUAL_TRANSLATIONS"));
}

@DataProvider
protected String[][] getCreateData() {
    return(TestingContext.getDataReader().getTableArray("multilingualcreate", "TRANSLATIONS"));
}

}

And finally, the TestSuite.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
    <suite name="WebAppSuiteTest" parallel="none">
      <parameter name="selenium.host" value="localhost" />
      <parameter name="selenium.port" value="5555" />
      <parameter name="selenium.browser" value="*firefox3 C:\\Documents and Settings\\dgarcia\\Local Settings\\Application Data\\Mozilla Firefox\\firefox.exe" />
      <parameter name="selenium.url" value="http://localhost:8080/standard-webapp-war/home.seam" />
      <parameter name="selenium.timeout" value="1000000" />
      <parameter name="test.data.filepath" value="src\\test\\resources\\datatest_.xls" />

  <test name="standard" preserve-order="true" >
    <classes>
        <class name="com.standard.webapp.common.TestingContext" />      
        <class name="com.standard.webapp.login.TestLogin"/>
        <class name="com.standard.webapp.TestLanguages"/>
        <class name="com.standard.webapp.TestTranslations"/>
        </class>
    </classes>
  </test>
</suite>

there is no exception nor any reason on the output to skip those tests. I am not really sure about the output you mention, so I will paste here the content of the generated "myWebAppTest.xml" with the results of the tests:

<testsuite hostname="SP2L0044" name="com.sicpa.standard.dms.webapp.selenium.common.BaseWebAppPageTest" tests="14" failures="0" timestamp="4 Mar 2011 08:45:57 GMT" time="27.141" errors="0">
  <testcase name="testLoginLoadHome" time="2.188" classname="com.sicpa.standard.dms.webapp.selenium.login.TestLogin"/>
  <testcase name="testCreateCorrect" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testFilter" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testUpdate" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testView" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes">
    <skipped/>
  </testcase>
  <testcase name="testCreateCorrect" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages">
    <skipped/>
  </testcase>
  <testcase name="testFilter" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages">
    <skipped/>
  </testcase>
  <testcase name="testModify" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages">
    <skipped/>
  </testcase>
  <testcase name="createCorrect" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations">
    <skipped/>
  </testcase>
  <testcase name="filter" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations">
    <skipped/>
  </testcase>
  <testcase name="update" time="0.0" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations">
    <skipped/>
  </testcase>
  <testcase name="testOpen" time="2.297" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestLanguages"/>
  <testcase name="testOpen" time="12.61" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestCodes"/>
  <testcase name="testOpen" time="9.469" classname="com.sicpa.standard.dms.webapp.selenium.multilingual.TestTranslations"/>
</testsuite>

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

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

发布评论

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

评论(10

故人爱我别走 2024-10-27 02:51:08

TestNG 跳过测试的原因有多种,最常见的原因是您所依赖的方法(例如 testOpen 或 createCorrect)以某种方式失败。

我建议将详细级别设置为 10 并将输出粘贴到此处或 testng-users 邮件列表上。

如果您使用的是 XML 定义,请设置详细级别,如下所示:。详细级别也可以通过代码配置来设置。

TestNG tng = new TestNG();
XmlSuite suite = new XmlSuite();

suite.setVerbose(10);

// other configuration, add tests to run

List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add( suite );
tng.setXmlSuites(suites);
tng.run();

There are various reasons why TestNG is skipping the tests, the most common one is that a method you depend on (e.g. testOpen or createCorrect) failed in some way.

I suggest setting the verbose level to 10 and paste the output here or on the testng-users mailing-list.

If you are using an XML definition, set the verbose level like so: <suite name="sweet_suite" verbose="10">. The verbosity level can also be set through code configuration.

TestNG tng = new TestNG();
XmlSuite suite = new XmlSuite();

suite.setVerbose(10);

// other configuration, add tests to run

List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add( suite );
tng.setXmlSuites(suites);
tng.run();
猫七 2024-10-27 02:51:08

DataProvider 方法中可能会抛出错误。

It could be error thrown in DataProvider method.

仙女 2024-10-27 02:51:08

如果您想在扩展类中运行测试,而不管依赖方法的通过/失败,您始终可以为扩展类中的测试添加“alwaysRun = true”,如下所示:

@Test(dependsOnMethods={"testOpen"}, dataProvider="getCreateData", alwaysRun = true)

If you would like to run the tests in extending classes irrespective of the pass/fail of the depending methods, you can always add 'alwaysRun = true' for the tests in extending classes like this:

@Test(dependsOnMethods={"testOpen"}, dataProvider="getCreateData", alwaysRun = true)
瑾夏年华 2024-10-27 02:51:08

我过去遇到过这个问题,我发现当我有两个 @BeforeMethod 注释时,一个在 test.class 内,一个在 basetest.class 内。顺便说一句,两者都做了不同的事情,但仍然发生了冲突 - 阻止了测试的执行并最终跳过。
底线:检查您是否没有任何类型的多个@Before、@After。

I had this problem in the past and I found out that when I have 2 annotations of @BeforeMethod, one inside the test.class and one inside a basetest.class. Both, by the way, did something different, still a collision happened - preventing the tests to be executed and eventually skip.
Bottom line: Check that you don't have multiple @Before, @After of any kind.

剪不断理还乱 2024-10-27 02:51:08

尽管看起来很愚蠢,但我忘记将 @dataProviderClass 添加到我的测试中。这就是为什么我的测试被跳过的原因。但testng并没有明确告诉我问题出在哪里。在尝试此处的其他答案之前,请务必先检查这些基础知识。

As silly as it seems, I had forgotten to add @dataProviderClass to my test. That is why my tests were getting skipped. But testng did not explicitly tell me where the issue was. Always check these basics first before trying the other answers here.

绻影浮沉 2024-10-27 02:51:08

如果您在 testNG 方法中接收参数但在执行时未传递这些参数,则也会跳过测试。
我最近遇到了这个问题,所以请仔细检查。

Test are also skipped if you are taking in parameters in testNG method but those are not getting passed while executing.
I recently faced this issue, so just double check.

旧伤还要旧人安 2024-10-27 02:51:08

原因之一是套件类中的优先级,使用带有线程计数的 testng 并行概念将解决您的问题。

像这样使用:
<套件名称=“TestSuite”详细=“1”并行=“测试”线程计数=“10”

One reason is priority inside the classes of suite, use parallel concepts of testng with thread count will solve your problem.

Use Like This:
<suite name="TestSuite" verbose="1" parallel="tests" thread-count="10"

一直在等你来 2024-10-27 02:51:08

造成这种情况的原因可能有多种,但在看到您的代码后,我注意到一件事,只需确保您已正确设置浏览器 exe,例如在 Chrome 上执行测试,您的路径中将需要 chromedriver.exe。

 System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+
                "\\src\\main\\resources\\chromedriver.exe");

There can be multiple reason for this but after seeing your code I noticed one thing, just make sure you've set the browser exe correctly, like to execute your tests on Chrome you would need chromedriver.exe in your path.

 System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+
                "\\src\\main\\resources\\chromedriver.exe");
萌面超妹 2024-10-27 02:51:08

我有同样的问题,测试被忽略。降级为 TestNG 的稳定版本后,它开始运行测试。

I had the same issue, test ignored. After degrading to stable version of TestNG it started running the test.

紫南 2024-10-27 02:51:08

大多数情况下,它会跳过那些依赖于其他测试结果或设置了一些超时或由于配置而导致的测试。

如果您设置了 @afterMethods@afterClass,则值得将它们注释掉一次并重新运行脚本。

您可以做的是为所有测试设置优先级,如果所有测试都依赖于某个测试,则将其保留在适合您的 @beforeMethod@beforeClass 中。

Mostly it skips those tests that depend on the result of other tests or which have some timeout set or due to configurations.

If you have set @afterMethods or @afterClass, it would be worth commenting them out once and re-running the script.

What you can do is set priority with all your test and if all your tests depend on a certain test, then keep that in @beforeMethod or @beforeClass which ever fits you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文