java - Junit 在 Eclipse 中通过,在 Jenkins 中失败

标签 java testing jenkins junit jenkins-plugins

您好,我的 JUnit 测试在 eclipse 中通过了。 JUnit 在 Eclipse 中有所有绿色条,但是当我从 Jenkins 调用 xml 文件时,它说失败。我已经在 stakeoverflow 上搜索了几个小时,但我找不到问题所在。请帮助我的有效代码如下:

   @FixMethodOrder(MethodSorters.NAME_ASCENDING) // this allows you to execute test in order 
public class AdpPortal_1_Homepage {

  private WebDriver driver;
  private String homeUrl;
  private String homeTitle = "ADP Associate Portal";
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\eclipse\\chromedriver\\chromedriver.exe");
    driver = new ChromeDriver();
    homeUrl = "https://myadp.adpcorp.com/wps/myportal/main/myADP_new";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  //Full test
  @Test
  public void fullTest() throws Exception {

    step01_VerifyHomePage();

  }


  // Go to Home page and verify title 
  public void step01_VerifyHomePage() throws Exception {
    driver.get(homeUrl);
    // homeTitle = "ADP Associate Portal" 
    driver.getTitle().contains(homeTitle);
    Thread.sleep(3000);
  }


  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
}

另外还有 Jenkins 的控制台输出:

enter image description here

请帮忙。

ant 文件片段:

 <target name="AdpPortal_1_Homepage">
        <mkdir dir="${junit.output.dir}"/>
        <junit fork="yes" printsummary="withOutAndErr">
            <formatter type="xml"/>
            <test name="AdpPortal_1_Homepage" todir="${junit.output.dir}"/>
            <classpath refid="ADP_Automation_(JUnit)_(Jenkins).classpath"/>
        </junit>
    </target>
    <target name="junitreport">
        <junitreport todir="${junit.output.dir}">
            <fileset dir="${junit.output.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${junit.output.dir}"/>
        </junitreport>

enter image description here

---------------- 新问题------------------------ enter image description here

@Before
    public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\eclipse\\chromedriver\\chromedriver.exe");  
    driver = new ChromeDriver();
    homeUrl = "https://myadp.adpcorp.com/wps/myportal/main/myADP_new";
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get(homeUrl);
    }

        //Full test
         @Test 
         public void fullTest() throws Exception {

             step02_HoverMyWorkSpace(); 

         }


          public void step02_HoverMyWorkSpace() throws Exception {
              WebDriverWait wait = new WebDriverWait(driver,20000);
              wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("DivMyWorkList"))));
              if ( driver.findElement(By.id("DivMyWorkList")).isDisplayed()){
                  System.out.println("DivMyWorkList Visiable");
              }else{
                  System.out.println("DivMyWorkList NOT Visiable");
              }

    //        Assert.assertEquals(true, workspace.isDisplayed());
    //        Thread.sleep(3000);
          }

最佳答案

Ant 构建中的 jUnit 步骤会为每个已执行的测试类创建包含测试结果的 XML 文件。

如果你的步骤是这样的

<target name="unit-test" depends="test-build" >
    <junit printsummary="yes" haltonfailure="no" haltonerror="no" failureproperty="test.failed">
        <classpath refid="test.classpath"/>
        <formatter type="xml" />
        <batchtest fork="no" todir="build/test-results">
            <fileset dir="${test-src}">
                <include name="**/*Test*.java" />
            </fileset>
        </batchtest>
    </junit>
  <fail message="Test failure detected, check test results." if="test.failed" />
</target>

然后这些文件将在您的项目目录下的 build/test-results 目录中生成。唯一的问题是目录 build/test-results 应该在执行构建之前手动创建,或者应该在之前的构建过程中创建。

关于java - Junit 在 Eclipse 中通过,在 Jenkins 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48815144/

相关文章:

Java 单独的 System.out 用于单独的类路径

jenkins - 在 Jenkins Job 的 config.xml 中管理插件版本

在 Jar 文件中找不到 JavaFx 图像

java - 使用 PVectors 使速度恒定

python - python 和 GUI 测试中的包内引用

testing - 如何在 Erlang 中进行单元测试覆盖

java - 使用 CDI-Weld 注入(inject)继承的属性值

java - 测试超时。什么时候发生?

ruby - 验证包含值时出现验证错误 :Rails

node.js - 对我的代码运行 Sonar 库分析时出现 NullPointerException