注释在Maven项目中不起作用
我希望每个人都进展顺利,我在src/test/java/com.selenium.test下进行了2个课程:
apptest.java:
package com.selenium.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class AppTest {
WebDriver driver;
//Before function
@Before
public void Begin()
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@After
public void End()
{
driver.quit();
}
}
和第二类Projectest.java:java:
package com.selenium.test;
import org.junit.Test;
public class Apptest2 extends AppTest{
@Test
public void testWebsite(){
System.out.println("Starting test " + new Object({}.getClass().getEnclosingMethod().getName());
driver.get("http://www.google.com");
}
在CMD中,我访问了项目文件夹,我写了此命令“ MVN测试”,这是我的输出:
C:\Users\user\eclipse-workspace\SeleniumTest>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.selenium:SeleniumTest >----------------------
[INFO] Building SeleniumTest 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ SeleniumTest ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.106 s
[INFO] Finished at: 2022-04-06T21:43:07+02:00
[INFO] ------------------------------------------------------------------------
这也是我的“ pom.xml”文件:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.selenium</groupId>
<artifactId>SeleniumTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</reporting>
问题是,当我将@test代码放在第一类中时:
package com.selenium.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class AppTest {
WebDriver driver;
//Before function
@Before
public void Begin()
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void testWebsite(){
System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
driver.get("http://www.google.com");
}
@After
public void End()
{
driver.quit();
}
}
当我编写命令MVN TEST时,我有这个输出:
-------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
但是,当我在第二类中写下@test时,如上所示,我有一个结果:
-------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
我不明白问题在哪里,任何人都可以帮助我。先感谢您
I hope everyone is going well, i have following 2 classes under src/test/java/com.selenium.test:
AppTest.java:
package com.selenium.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class AppTest {
WebDriver driver;
//Before function
@Before
public void Begin()
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@After
public void End()
{
driver.quit();
}
}
and the second class Projectest.java:
package com.selenium.test;
import org.junit.Test;
public class Apptest2 extends AppTest{
@Test
public void testWebsite(){
System.out.println("Starting test " + new Object({}.getClass().getEnclosingMethod().getName());
driver.get("http://www.google.com");
}
in cmd i access to the project folder and i write this command "mvn test", this is the output that i have:
C:\Users\user\eclipse-workspace\SeleniumTest>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.selenium:SeleniumTest >----------------------
[INFO] Building SeleniumTest 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ SeleniumTest ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.106 s
[INFO] Finished at: 2022-04-06T21:43:07+02:00
[INFO] ------------------------------------------------------------------------
this is also my "pom.xml" file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.selenium</groupId>
<artifactId>SeleniumTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
</plugins>
</reporting>
The problem is that when i put the @test code in the first class like this:
package com.selenium.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class AppTest {
WebDriver driver;
//Before function
@Before
public void Begin()
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void testWebsite(){
System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
driver.get("http://www.google.com");
}
@After
public void End()
{
driver.quit();
}
}
when i write the command mvn test, i have this output:
-------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
but when i wrote the @test in the second class as shown above, i have this resultat:
-------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
I didn't understand where the problem is, can anyone please help me. Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您需要首先运行“ MVN CLEAN COMPILE”(用使用“我运行” MVN CLEAN安装')。我猜问题是您的新课程没有重新编译。我看到
[info] - -Maven-Compiler-Plugin:3.1:testCompile(默认testCompile ) @ seleniumtest
[infor]没有编译 - 所有类都是最新的
perhaps you need to run "mvn clean compile" first (usally I run "mvn clean install' all the time). I guess problem is that your new class with new @Test annotation was not recompiled. I see that
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SeleniumTest
[INFO] Nothing to compile - all classes are up to date
第一个
src/test/java/com.selenium.test
这不是一个正确的包src/test/java/com/selenium/test
第二个名称Apptest2
不遵循命名约定。它必须以*Test.java
结尾,并注意大小写,因此将其更改为AppTwoTest
... 而...并且您应该重命名您的AppTest 因为它本身不是测试,它只是其他测试的基类,例如
AppTestBase
First
src/test/java/com.selenium.test
this is not a correct packagesrc/test/java/com/selenium/test
second the nameApptest2
does not follow naming convention. It must end with*Test.java
and pay attention to case so change it intoAppTwoTest
... instead ... And you should rename yourAppTest
because it's not a test itself it's only a base class for other tests so for exampleAppTestBase
在我看来,您根本没有配置Maven-Surefire-Plugin来找到您的测试类。
另外,您可能需要使用“硒jupiter”,而不是在POM中使用的配置。只是我的意见。
以下是示例: https://github.com/bonigarcia/benigarcia/selenium-jupiter-jupiter-examples
Looks to me like you simply haven't configured maven-surefire-plugin to be able to find your Test classes.
Also, you might want to use "Selenium-Jupiter" instead of the configuration you have in your Pom. Just my opinion.
Here are the examples: https://github.com/bonigarcia/selenium-jupiter-examples