当JUnit类具有多个导入时,如何从Windows CMD窗口中运行JUnit测试? [英] How do I run JUnit tests from a Windows CMD window when the JUnit class(es) have multiple imports?

查看:103
本文介绍了当JUnit类具有多个导入时,如何从Windows CMD窗口中运行JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的JUnit测试课程: 包com.bynarystudio.tests.storefront;

Here is my JUnit test class: package com.bynarystudio.tests.storefront;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.bynarystudio.tests.BaseWebTest;

public class SmokeTests extends BaseWebTest {

@Test
public void StoreFrontMegaNavTest(){
    webDriver.get(default_homepage);

    String source = webDriver.getPageSource();

    Assert.assertTrue(source.contains("some text"));    
}
}

如何从命令行运行此测试?当我尝试使用

How do I run this test from the command line? When I try to run it from inside its directory using

java -cp junit.textui.TestRunner SmokeTests.java

我收到以下错误

Could not find the main class: SmokeTests.java.  Program will exit.

我认为这与我的类路径设置不正确有关.但是我不知道,因为我是Java的新手.来自.NET,C#和Visual Studio,整个类路径都是毫无意义的.即使我已将所有文件正确添加到Eclipse中的项目中(我知道这是因为该测试可以从Eclipse内部正常运行),但它绝对不会从命令行运行或编译.

I think this has to do with the fact that my classpath is not setup properly. But I have no idea because I'm brand new to Java. Coming from .NET, C#, and Visual Studio, the whole classpath thing makes no sense. Even though I have all my files correctly added to a project in Eclipse (I know because the test runs fine from inside Eclipse), it will absolutely not run or compile from the command line.

推荐答案

首先,您要混合两件事:

First of all you are mixing two things:

  1. 首先,您必须使用javac命令来编译项目.结果,您将获得一组.class文件(不是.java->这是源代码)

  1. First you have to compile the project using javac command. As a result you will get set of .class files (not .java -> this is source code)

然后,您可以使用java命令运行代码: java -cp classPath yourpackage.SmokeTests

Then you can run the code using java command: java -cp classPath yourpackage.SmokeTests

其中:

classPath -是已编译类所在的目录或jar文件的列表,如果您添加多个条目,请使用;"将它们分开(windows)或:"(Linux)

classPath - is the list of directories or jar files where your compiled classes are, if you jave multiple entries separate them using ";" (windows) or ":"(Linux)

因此您的classPath可以是:-cp.; c:/jars/*; deps

so your classPath can be: -cp .;c:/jars/*;deps

这意味着您的类路径将包含:

which means your classpath will contain:

  • 当前目录
  • 来自c:/jars/*
  • 的所有jar文件
  • 工作目录中deps目录中的所有jar文件
  • current directory
  • all jar files from c:/jars/*
  • all jar files from deps directory that is in your working dir

因此完整的命令可以是:

So the full command can will be:

java -cp.; c:/jars/*;降低SmokeTests

java -cp .;c:/jars/*;deps SmokeTests

yourpackage -是SmokeTests类的软件包,如果您在SmokeTests.java中没有定义软件包,请将其留空

yourpackage - is the package of the SmokeTests class, if you do not have package defined in the SmokeTests.java leave this empty

这篇关于当JUnit类具有多个导入时,如何从Windows CMD窗口中运行JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆