首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:包org.testng.annotations不存在[javac]导入org.testng.annotations.Test;

错误:包org.testng.annotations不存在[javac]导入org.testng.annotations.Test;
EN

Stack Overflow用户
提问于 2015-09-21 22:33:22
回答 2查看 12.1K关注 0票数 3

我有一个简单的项目,我正在尝试使用Ant构建。我的项目包含testng,但我在尝试运行构建时收到一条错误消息。你能帮帮我吗?我花了很多时间试图找出问题所在,但到目前为止还没有任何结果。

下面是这个项目:

代码语言:javascript
复制
package selenium;

import org.testng.annotations.Test;

public class HomePage {

    @Test
    public void test(){

        System.out.println("this is test");
    }
}

现在,我有了一个包含testng.jar的lib文件,还有一个运行该类的test.xml套件,下面是我的构建文件:

代码语言:javascript
复制
    <?xml version="1.0" encoding="UTF-8"?>

<project name="Concep.TestAutomation" basedir="." default="all">

	<property name="src.dir" value="${basedir}/src" />
	<property name="build.dir" value="target" />
	<property name="classes.dir" value="${build.dir}/classes" />	
	<property name="suites.dir" value="suites" />
	<property name="testng.path" value="lib/testng.jar" />
	
	<property name="testreport.dir" value="${build.dir}/test-output" />
	
	<target name="clean" description="Delete the build directory.">
		<delete dir="${build.dir}" />
	</target>

	<target name="init" depends="clean" description="Create build directories.">
		<!--Create the build directory structure used by compile -->
		<mkdir dir="${build.dir}" />
		<mkdir dir="${classes.dir}" />
		<mkdir dir="${testreport.dir}" />
	</target>

	<target name="compile" depends="init" description="Compile tests.">
		<!-- Compile the java code from ${src} into ${classes.dir} -->
		<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" />
	</target>

	<target name="run-test" depends="compile" description="Run test suite.">
		<!-- Run a testng suite-->
		<taskdef resource="testngtasks" classpath="${testng.path}" />

		<testng classpathref="${testng.path}" outputDir="${testreport.dir}" haltonfailure="true" verbose="5">
			<xmlfileset dir="${suites.dir}" includes="IA-smoke-test.xml" />
			</testng>
	</target>

	<target name="all" depends="run-test" description="Executes all targets." />
	
		<target name="all-debug" description="outputs all vars in use.">
  <!-- useful to diagnose runtime problems --> 
       <echo message="'work.dir' = '${basedir}/..'"/>
       <echo message="'lib.dir' = '${basedir}/lib'"/>
       <echo message="'src.dir' = '${basedir}/src'"/>
       <echo message="'config.dir' = '${basedir}/config'"/>
       <echo message="'build.dir' = 'target'"/>
       <echo message="'classes.dir' = '${build.dir}/classes'"/>
       <echo message="'suites.dir' = '${basedir}/suites'"/>
       <echo message="'testng.path' = '${basedir}/lib/testng.jar'"/>
     </target>

</project>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-25 15:19:41

所以我在这里修复了这个问题,在编译目标上缺少了一个属性,它是"classpathref",从那里项目可以看到编译它所需的所有库,如果没有"classpathref“,项目看不到任何外部系统,所以我做了以下修复,

代码语言:javascript
复制
<path id="build.classpath">
		<fileset dir="${lib.dir}" includes="**/*.jar" />
		<fileset dir="${src.dir}/concep/selenium/core" includes="**/*.java" />
	</path>

在我的compile目标中,我添加了以下属性

代码语言:javascript
复制
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="build.classpath" debug="true" includeantruntime="false"/>

票数 0
EN

Stack Overflow用户

发布于 2017-02-10 17:56:02

甚至我也曾面对过这个问题,并得到了同样的解决方案。

在您的pom.xml文件中,删除作用域,这应该可以很好地工作。

按照pom.xml中的以下代码,删除作用域标记。

尽管我们已经为Testng导入了maven依赖项,但当您在XML文件中添加scope标记时,它会被视为JUnit注释,而不是Testng。因此,当我删除作用域标记时,我的@Test Annotation被视为Testng Annotation。

代码语言:javascript
复制
<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
            **<scope>test</scope>** //Remove this line and compile maven
</dependency>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32697906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档