在工作中,我们目前仍在使用JUnit 3来运行我们的测试.我们一直在考虑切换到JUnit 4进行新的测试,但我一直在关注TestNG.您对JUnit 4或TestNG有什么经验,对于大量的测试似乎更好?编写测试的灵活性对我们来说也很重要,因为我们的功能测试涵盖了广泛的方面,需要以各种方式编写才能获得结果.
旧的测试不会被重写,因为他们的工作做得很好.我希望在新测试中看到的是测试编写方式的灵活性,自然断言,分组和易于分布的测试执行.
我有一个抽象基类,我用它作为单元测试的基础(TestNG 5.10).在这个类中,我初始化我的测试的整个环境,设置数据库映射等.这个抽象类有一个带有@BeforeClass注释的方法来进行初始化.
接下来,我使用特定的类扩展该类,其中我有@Test方法和@BeforeClass方法.这些方法对类环境进行特定于类的初始化(例如,将一些记录放入数据库中).
我如何强制执行@BeforeClass注释方法的特定顺序?我需要抽象基类中的那些在扩展类之前执行.
例:
abstract class A {
@BeforeClass
doInitialization() {...}
}
class B extends A {
@BeforeClass
doSpecificInitialization() {...}
@Test
doTests() {...}
}
Run Code Online (Sandbox Code Playgroud)
预期订单:
A.doInitialization
B.doSpecificInitialization
B.doTests
Run Code Online (Sandbox Code Playgroud)
实际订单:
B.doSpecificInitialization // <- crashes, as the base init is missing
(A.doInitialization // <---not executed
B.doTests) // <-/
Run Code Online (Sandbox Code Playgroud) JUnit 4和TestNG曾经具有可比性.两个测试框架的优缺点是什么?
如何自定义TestNG中测试的执行顺序?
例如:
public class Test1 {
@Test
public void test1() {
System.out.println("test1");
}
@Test
public void test2() {
System.out.println("test2");
}
@Test
public void test3() {
System.out.println("test3");
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的套件中,测试的执行顺序是任意的.对于一次执行,输出可以是:
test1
test3
test2
Run Code Online (Sandbox Code Playgroud)
如何按照编写顺序执行测试?
这是我在JUnit中可以做的:
import org.junit.*;
@Ignore
public class FooTest {
//
}
Run Code Online (Sandbox Code Playgroud)
整个班级都会被忽略.我怎么能在TestNG中做同样的事情?
Spring支持JUnit:使用RunWith和ContextConfiguration注释,事情看起来非常直观
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:dao-context.xml")
Run Code Online (Sandbox Code Playgroud)
该测试将能够在Eclipse和Maven中正确运行.我想知道TestNG是否有类似的东西.我正在考虑转向这个"下一代"框架,但我没有找到与Spring测试的匹配.
我正在使用Selenium WebDriver(Java)和TestNG在我创建的网站上进行一些测试.在这个网站上,我也有JavaScript,在一些函数中,它返回值并通过输出值输出到浏览器控制台console.log().
我想知道Selenium WebDriver是否有一种简单的方法可以访问这些JavaScript信息,因此我可以使用TestNG执行断言.
我对Selenium很新,但我知道你可以这样做:
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("btn")).click();
Run Code Online (Sandbox Code Playgroud)
那么我可以用什么类似的东西WebDriver来阅读网站上的JavaScript?
看起来人们假设我正试图通过Selenium"执行"JavaScript代码.
不是这样的.相反,我正在尝试使用Selenium存储已定义的JavaScript变量.
基本上,我希望Selenium能够获取JavaScript变量的值,将其存储在本地,然后对其进行断言测试.
说我的网站有以下JS代码:
$(document).ready(function() {
var foo = $(#"input-field-val").val();
function returnFoo() {
return foo;
}
});
Run Code Online (Sandbox Code Playgroud)
根据我的阅读和理解,在我单独的Selenium测试文件(Selenium.java)中,我应该可以做这样的事情吗?:
public class Selenium {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
@Test
public void testSample() {
driver.get("www.mywebsite.com");
js.executeScript("alert(returnFoo());");
}
}
Run Code Online (Sandbox Code Playgroud)
我做了类似于上面的事情,但没有弹出警报框.相反,我收到一条错误消息:
Exception in thread "main" org.openqa.selenium.WebDriverException: ReferenceError: returnFoo is not defined
Run Code Online (Sandbox Code Playgroud)
当我说JS变量时,我很确定我不明白它意味着什么
不应该是闭包或局部变量的一部分
我也尝试在上面定义一个全局变量,$(document).ready(function()...并且设置在function returnFoo()但仍然不起作用.
我已经移动了两个 …
下面的机制是什么使得不同的类型?
import static org.testng.Assert.assertEquals;
Run Code Online (Sandbox Code Playgroud)
@Test
public void whyThisIsEqual() {
assertEquals(new HashSet<>(), new ArrayList<>());
}
Run Code Online (Sandbox Code Playgroud) 堆栈跟踪被截断 - 例如它们以 [info] ...
使用last或更改traceLevel没有帮助 - 它只是打印sbt包装器的完整堆栈跟踪.
这是用testng测试的(我也相信使用scalatest和sl4j)
我正在使用TestNG测试业务服务,在春季启动应用程序中进行模拟单元测试.
应用程序是多模块弹簧启动项目.我正在为业务模块编写单元测试.
我在pom中添加了以下依赖关系测试,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>${javaxel.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>${javax.servlet.version}</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的包装器注释看起来像
@Service
@Transactional
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyServiceAnnotation{}
Run Code Online (Sandbox Code Playgroud)
我的TestApp看起来像
@SpringBootApplication
public class TestApp{ .... }
Run Code Online (Sandbox Code Playgroud)
我的商业服务看起来像
@MyServiceAnnotation
public class AddressServiceImpl implements AddressService {
@Autowire
UserDAO userDAO;
@Autowire
AddressDAO addressDAO;
public Address find(int userId) …Run Code Online (Sandbox Code Playgroud) testng ×10
java ×5
junit ×4
unit-testing ×4
testing ×2
collections ×1
javascript ×1
mockito ×1
sbt ×1
scala ×1
scalatest ×1
selenium ×1
slf4j ×1
spring ×1
spring-boot ×1