简体   繁体   中英

I'm running testng from command line but no tests get executed

If I run my testng.xml from Eclipse IDE, everything works well; but if aI try to run them from command line then i see following 在此处输入图片说明

Folder structure: 在此处输入图片说明

My Class is following, whose test are run fine when I run from eclipse IDE:

package package1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Example1 {

    public static WebDriver driver;

    @BeforeTest
    public void launchBrowser() {
        System.setProperty("webdriver.chrome.driver", "C:\\Eclipse\\Selenium\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test(priority = 1)
    public void verifygoogleTitle() {
        driver.get("http://www.google.com");
        Assert.assertEquals("Google", driver.getTitle());
    }

    @Test(priority = 2)
    public void verifyyahooTitle() {
        driver.get("https://in.yahoo.com");
        Assert.assertEquals("Yahoo", driver.getTitle());
    }

    @Test(priority = 3)
    public void verifybankofindiaTitle() {
        driver.get("http://www.bankofindia.co.in/english/home.aspx");
        Assert.assertEquals("Bank Of India - Home", driver.getTitle());
    }

    @AfterClass
    public void closeBrowser() {
        driver.close();
    }

}

My testng.xml is following:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="package1.Example1"/>

    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

[UPDATE] Extended Logs :

在此处输入图片说明

I had similar problem. testng.xml was running fine if I run it from Eclipse but when I run the same from command prompt ; it gave the result as 0 tests run, there was no errors.

My issue was in my classpath setting. I had kept all my TestNG jar files in a separate folder (to organize better) in eclipse project's lib directory while the rest of my jar files were directly in the lib directory. When I added the lib as well to my classpath it worked fine.

set classpath = D:\\workspace\\SeleniumTrials\\bin;D:\\workspace\\SeleniumTrials\\lib\\TestNG*;D:\\workspace\\SeleniumTrials\\lib* java org.testng.TestNG testng.xml

Let me address your question here with a few points:

  1. If at all we are using maven, there is no need to install (add TestNG libraries) TestNG seperately (manually) either by Install New Software or Eclipse Market Place . Instead we can simply specify the TestNG dependency in the pom.xml as follows:

 <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope> </dependency> 

Advantages: maven-surefire-plugin does all the downloading & configuration for us.

  1. Now, we have got 4 optional ways to execute the Testcase as follows:

A. Run As TestNG Test (Eclipse, through .java file)

B. Run As TestNG Suite (Eclipse, through testng.xml)

C. Run As maven test (Eclipse, through pom.xml)

D. Command-line option.

For command line execution: a. We have to specify in pom.xml where to find the testng.xml . For example:

 <suiteXmlFiles> <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> </suiteXmlFiles> 

To achieve this,

b. We have to move the testng.xml to src/main/resources

c. Through command-line change directory to project folder.

d. Flush out previous dependencies.

e. Install the necessary dependencies.

f. Install build & execute testcase.

Let me know if this answers your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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