JUnit测试通过但在使用Maven运行时失败

时间:2019-04-02 17:01:28

标签: java maven junit

我有一个在JUnit中运行时通过的测试,但是在与Maven一起运行时在我的机器和CI系统中失败。更有趣的是,它传递给使用Windows的另一台同事计算机。

有人对为什么会这样有任何想法吗? 总结一下:

MacOS Mojave

  • Java版本:1.8.0_131
  • Maven版本:3.6.0
  • JUnit测试执行:通过
  • Maven测试执行:失败

Windows 10

  • Java版本:有点不同(没有确切的数字)
  • Maven版本:3.6.0
  • JUnit测试执行:通过
  • Maven测试执行:通过

CI(特拉维斯)

  • Linux
  • Java版本: 1.8.0_151
  • Maven版本:3.6.0
  • JUnit测试执行:不适用
  • Maven测试执行:失败

CI的错误日志

 T E S T S
-------------------------------------------------------
Running com.saucelabs.common.JavaScriptInvokerTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.295 sec
Running com.saucelabs.common.SauceHelperTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec
Running com.saucelabs.common.SauceHelperAcceptanceTest
Mar 29, 2019 9:14:20 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Mar 29, 2019 9:14:30 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 19.581 sec <<< FAILURE!
shouldSetTestStatusToPassed(com.saucelabs.common.SauceHelperAcceptanceTest)  Time elapsed: 7.969 sec  <<< FAILURE!
java.lang.AssertionError: expected:<true> but was:<false>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com.saucelabs.common.SauceHelperAcceptanceTest.shouldSetTestStatusToPassed(SauceHelperAcceptanceTest.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Running com.saucelabs.common.JavaScriptInvokerFactoryTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec
Results :
Failed tests:   shouldSetTestStatusToPassed(com.saucelabs.common.SauceHelperAcceptanceTest): expected:<true> but was:<false>
Tests run: 10, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] sauce_java ......................................... SUCCESS [  0.003 s]
[INFO] sauce_java_common .................................. FAILURE [ 32.230 s]
[INFO] sauce_junit ........................................ SKIPPED
[INFO] quickstart-archetype ............................... SKIPPED
[INFO] quickstart-webdriver-junit ......................... SKIPPED
[INFO] quickstart-webdriver-testng ........................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 41.039 s
[INFO] Finished at: 2019-03-29T21:14:32Z
[INFO] Final Memory: 27M/220M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project sauce_java_common: There are test failures.
[ERROR] 
[ERROR] Please refer to /home/travis/build/saucelabs/sauce-java/common/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :sauce_java_common
The command "mvn test -B -V" exited with 1.

测试

public class SauceHelperAcceptanceTest
{
    private String username = System.getenv("SAUCE_USERNAME");
    private String accesskey = System.getenv("SAUCE_ACCESS_KEY");
    String SAUCE_REMOTE_URL = "https://ondemand.saucelabs.com/wd/hub";
    private WebDriver driver;
    SessionId sessionId;

    @Rule
    public TestName testName = new TestName();

    @Before
    public void runBeforeEachTest() throws MalformedURLException {
        ChromeOptions caps = getChromeOptions();
        MutableCapabilities sauceOptions = getMutableCapabilities();
        caps.setCapability("sauce:options", sauceOptions);
        driver = new RemoteWebDriver(new URL(SAUCE_REMOTE_URL), caps);
        sessionId = ((RemoteWebDriver) driver).getSessionId();

        driver.navigate().to("https://www.saucedemo.com");
    }

    @Test
    public void shouldSetTestStatusToPassed(){

        SauceHelper sauceHelper = new SauceHelper(driver);
        sauceHelper.setTestStatus("passed");
        driver.quit();

        String jobInfo = getSauceJobInformation();
        assertAcceptanceTestPassed(jobInfo);
    }

    private void assertAcceptanceTestPassed(String jobInfo) {
        Boolean isTestPassed = checkIfTestPassed(jobInfo);
        Assert.assertTrue(isTestPassed);
    }

    private String getSauceJobInformation() {
        SauceREST sauceRest = new SauceREST(username, accesskey, DataCenter.US);
        return sauceRest.getJobInfo(sessionId.toString());
    }

    @Test
    public void shouldSetTestStatusToPassedWithSeleniumJSExecutor(){
        ((JavascriptExecutor)driver).executeScript("sauce:job-result=passed");
        driver.quit();

        String jobInfo = getSauceJobInformation();
        assertAcceptanceTestPassed(jobInfo);
    }

    @After
    public void afterTest()
    {
        if(driver != null){
            driver.quit();
        }
    }

    private MutableCapabilities getMutableCapabilities() {
        MutableCapabilities sauceOptions;
        sauceOptions = new MutableCapabilities();
        sauceOptions.setCapability("username", username);
        sauceOptions.setCapability("accessKey", accesskey);
        sauceOptions.setCapability("seleniumVersion", "3.141.59");
        sauceOptions.setCapability("name", getTestName());
        return sauceOptions;
    }

    private ChromeOptions getChromeOptions() {
        ChromeOptions caps;
        caps = new ChromeOptions();
        caps.setCapability("version", "72.0");
        caps.setCapability("platform", "Windows 10");
        caps.setExperimentalOption("w3c", true);
        return caps;
    }
    private Boolean checkIfTestPassed(String jobInfo) {
        Boolean isPassed;
        try
        {
            isPassed = JsonPath.from(jobInfo).getBoolean("passed");
        }
        catch(NullPointerException e)
        {
            isPassed = false;
        }
        return isPassed;
    }
    public String getTestName()
    {
        return this.getClass().getSimpleName() + " " + testName.getMethodName();
    }
    //TODO need a test that doesn't set the test status with JS executor, in which case "passed" should be null
}

源代码:https://github.com/saucelabs/sauce-java/tree/js2

0 个答案:

没有答案