简体   繁体   中英

How to run JUnit test from command prompt, without a main function

I want to run a simple JUnit test with Jenkins, and first I want to try it in command prompt, if it works.

The JUnit.jar file is in C:\\junit.jar

I have a dynamic web project, in which I have a "test" package in which I have a LoginTest class

    package test;
    import junit.framework.TestCase;
    public class LoginTest extends TestCase { 
        @Test
        public void testLogin() {
            ....
            assertstuff...
        }
    }

I have a separate project with a TestSuite class :

    package test;
    import org.junit.extensions.cpsuite.ClasspathSuite;
    import org.junit.runner.RunWith;
    @RunWith(ClasspathSuite.class)
    public class TestSuite {

    }

Then I added my LoginTest class to the TestSuite build path.

If I run the TestSuite with JUnit within Eclipse it works, but I want to run it from cmd.

I tried the following in cmd :

    java -cp C:\junit.jar;D:\Documents\eclipse\blablabla\project_path\bin test.TestSuite

but it's not working. Could you help me with this issue?

Read your command line carefully:

java -cp C:\junit.jar;... test.TestSuite
#        (classpath)      (main class)

You're telling Java to run your TestSuite's main method, which doesn't exist. Now compare the one from the SO question that Jens linked:

java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
#        (classpath)               (main class - in JUnit!)   (first argument)

Rather than trying to run TestSuite , the second command asks java to run JUnitCore , which has a main method java can call . After that class name, pass your test suite as the first parameter, which tells JUnitCore to load and run that class in particular.

Try this:

java -cp C:\junit.jar;D:\Documents\eclipse\blablabla\project_path\bin org.junit.runner.JUnitCore test.TestSuite
#        (your full classpath)                                        (main class)               (your test class)

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