简体   繁体   中英

Selenium junit.framework.AssertionFailedError:No Tests Found

I am trying to run a quick selenium example to get started, and am having trouble. I have written JUnit test cases before and they work fine, but here's my code and error.

package alltests;
import testsuites.SeleniumTestTutorial;
import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests  {

public static Test suite() {
    TestSuite suite = new TestSuite("Test for SeleniumTutorial");
    suite.addTestSuite(SeleniumTestTutorial.class);
    return suite;
    }
}

Here's an old tutorial I am using. I didn't write these tests, they will most likely fail, I'm just trying to get them to run.

package testsuites;
import junit.framework.TestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;


public class SeleniumTestTutorial extends TestCase{

private static Selenium browser;

@BeforeClass
public static void init() {
    browser = new DefaultSelenium("localhost", 4444, "*firefox",
            "http://new.music.yahoo.com/");
    browser.start();
}

@Test
public void isVideosHpLoaded() {
    browser.open("http://new.music.yahoo.com/");
    browser.click("//*[@id=\"YMusic_HeaderNavItemMenu2\"]/a/span");
    browser.waitForPageToLoad("5000");
    System.out.println("checking title " + browser.getTitle());
    assertEquals("Music Videos on Yahoo! Music", browser.getTitle());
}

@Test
public void isTop100VideosModuleLoaded() {
    Number numElements = browser
            .getXpathCount("//*[@id=\"ymMvHpTopVideos\"]/div/div/h2/a");
    String modHeaderText = browser
            .getText("//*[@id=\"ymMvHpTopVideos\"]/div/div/h2/a");
    assertEquals(1, numElements.intValue());
    assertEquals("Top 100 Videos", modHeaderText);
}

@Test
public void isVideoStationsModuleLoaded() {
    Number numElements = browser
            .getXpathCount("//*[@id=\"ymMvHpVideoStationsContentContainer\"]/div/div[2]/h2/a");
    String modHeaderText = browser
            .getText("//*[@id=\"ymMvHpVideoStationsContentContainer\"]/div/div[2]/h2/a");
    assertEquals(1, numElements.intValue());
    assertEquals("Video Stations", modHeaderText);
}

@Test
public void countTotalVideoRecs() {
    Number numElements = browser
            .getXpathCount("//*[@id=\"ymusicRecommendHp\"]//ul[@class=\"ymusic_thumbnailList\"]/li");
    assertEquals(6, numElements.intValue());
}

@AfterClass
public static void cleanup() {
    browser.stop();
}
}

Here's the error I am getting. I've used this format for other JUnit tests, and I have never had any problems. I also can't seem to find an updated tutorial for using JUnit and Selenium. If anyone has any good links I would not be opposed. Thanks in advance!

junit.framework.AssertionFailedError: No tests found in testsuites.SeleniumTestTutorial
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at     org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I think it's because you're mixing JUnit 3 and JUnit 4 style tests.

If you're using JUnit 3.x, you want to extend TestCase, as you have, but then you need all your test methods to be named "testXYZ" (ie starting with the word test). The @Test annotations are ignored.

For JUnit 4, you don't extend TestCase, and you use the annotations.

You are mixing a JUnit 3 Test Suite and JUnit 4 test classes. You need to create a JUnit 4 test suite:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        SeleniumTestTutorial.class
})
public class AllTests {
}

When you execute a TestSuite (JUnit 3), it searches for methods which start with 'test'. For a JUnit 4 suite (with @RunWith(Suite.class) ), then it searches for methods with the @Test annotation.

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