How to run JUnit test cases from the command line

前端 未结 11 1589
花落未央
花落未央 2020-11-22 02:09

I would like to run JUnit test cases from the command line. How can I do this?

11条回答
  •  自闭症患者
    2020-11-22 02:45

    For JUnit 5.x it's:

    java -jar junit-platform-console-standalone-.jar 
    

    Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher

    For JUnit 4.X it's really:

    java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
    

    But if you are using JUnit 3.X note the class name is different:

    java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]
    

    You might need to add more JARs or directories with your class files to the classpath and separate that with semicolons (Windows) or colons (UNIX/Linux). It depends on your environment.

    Edit: I've added current directory as an example. Depends on your environment and how you build your application (can be bin/ or build/ or even my_application.jar etc). Note Java 6+ does support globs in classpath, you can do:

    java -cp lib/*.jar:/usr/share/java/junit.jar ...
    

    Hope it helps. Write tests! :-)

提交回复
热议问题