我正在使用JUnit 4.我看不出构造函数初始化或使用注释的专用init函数之间的区别@Before
.这是否意味着我不必担心它?
有没有什么@Before
比在构造函数中进行初始化更多的情况?
我明白那个,
@Before
并分别@BeforeClass
在每个测试或整个测试类之前运行@Rule
并分别@ClassRule
包装每个测试或整个测试类.假设我需要在每个测试方法之前初始化一些数据,
我如何决定使用@Before
和@Rule
?在什么条件下优先于另一种?同样的问题也无二@BeforeClass
对比@ClassRule
.
我使用Java,Selenium,Junit,Maven开发了一整套自动化测试.
对于每个测试,它们都有一个或多个@Category注释,描述每个测试涵盖的软件区域.例如:
@Test
@Category({com.example.core.categories.Priority1.class,
com.example.core.categories.Export.class,
com.example.core.categories.MemberData.class})
@Test
@Category({com.example.core.categories.Priority1.class,
com.example.core.categories.Import.class,
com.example.core.categories.MemberData.class})
@Test
@Ignore
@Category({com.example.core.categories.Priority2.class,
com.example.core.categories.Import.class,
com.example.core.categories.MemberData.class})
Run Code Online (Sandbox Code Playgroud)
我正在尝试做的是找到一种方法来计算包含任何给定类别的测试数量.所有可能的类别都是文件//com/example/core/categories
夹中的文件名作为源列表.
我已经尝试构建一个shell脚本来进行单词计数,这似乎工作正常,但我认为会有更多"内置"来处理@Category.
我最大的问题是,即使我得到正确的计数,很可能一个或多个测试被标记为@Ignore,这应该使测试@ Category的无效但没有大量使用标志并逐行读取每个文件为了它丢掉正确的计数.
是否有一个很好的方法来逐项列出@Ignore中的@ Category?
示例输出
| Category | Count |
|----------------------------------------------|------:|
| com.example.core.categories.Export.class | 1 |
| com.example.core.categories.Import.class | 1 |
| com.example.core.categories.MemberData.class | 2 |
| com.example.core.categories.Priority1.class | 2 |
| com.example.core.categories.Priority2.class | 0 |
| com.example.core.categories.Priority3.class | 0 |
Run Code Online (Sandbox Code Playgroud) 我正在运行测试Spring Boot应用程序的JUnit测试.我有一个@Before
方法和@After
一个.然后我有一堆@Test
方法,它们是实际的测试.
但是,我@Before
和@After
方法分别在每次测试之前和之后执行,而不是在所有测试之前执行一次,并且在所有测试之后执行一次.
难道我也在使用这个注释吗?
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
Run Code Online (Sandbox Code Playgroud) 我有一些依赖于环境变量的单元测试。
我想在使用 Maven 测试之前取消设置这些环境变量。
问题:我怎样才能实现这一目标?
在 Jest 和其他测试框架中?写beforeAll有什么意义呢?
以下链接没有帮助:
@Before、@BeforeClass、@BeforeEach 和 @BeforeAll 之间的区别
beforeAll(() => server.listen());
上面的行被放置在我正在检查的代码库的全局范围内。
如果我按如下方式重写它会发生什么变化?
server.listen()
我最近正在查看一个同事代码,我意识到他在describe调用顶部的BeforeAll函数中实现了一个笑话功能,然后在beforeEach函数中创建了一个数据对象。这让我想知道,BeforeAll和BeforeEach之间到底有什么区别。
是时候了...我去了谷歌!!我确实找到了一些文章,这些文章有助于阐明两者之间的某些功能差异。
调查结果1:http : //breazeal.com/blog/jasmineBefore.html
调查结果2:@ Before,@ BeforeClass,@ BeforeEach和@BeforeAll之间的区别
给定这些文章,我发现一次调用了AllAll一次。而在每个单独的测试之前调用BeforeEach。太好了!现在,我对何时调用它有了更好的了解!
我还发现,BeforeAll最适合用于初始化代码。这是完全合理的!初始化一次。繁荣,您完成了。
我的困惑是什么时候初始化什么东西,什么时候不初始化?我发现我们代码中的BeforeEach经常被使用。我很好奇的是哪种代码被认为是“初始化”代码,而不是BeforeEach中应该包含什么代码。
下面的代码示例:
beforeAll((done) => {
// Mocking method from within Box file
transferBoxPlanSpy = jest.spyOn(Box, 'transferPlanFromBox').mockImplementation(() => Promise.resolve());
// Pulling data from MongoDB
User.findOne({ user_name: 'testsurgeon1' }, (err, user) => {
user.addMGSPermission();
user.save(done);
});
});
beforeEach(() => {
planData2 = {
user_name: 'hello1',
laterality: 'right',
plan_id: 'testplan42',
order_number: '856-hd-02-l',
file_id: '123456sbyuidbefui',
};
});
Run Code Online (Sandbox Code Playgroud)
我希望我的问题不要太含糊。感谢您的时间!
编辑1 我想指出的是,该代码不是我自己编写的,而是由软件团队中的一位成员编写的。他将对象放在BeforeEach内部,并将模拟对象放在BeforeAll内部。
我的困惑是,似乎所有代码都可以放到BeforeAll中,只有少数例外。
我有一个简单的类,其中包含一个列表:
public class SomeClass {
private AppDataSource appDataSource; // it's interface
private List<Object> someList;
////
public List<Obejct> loadSomeList() {
if (someList == null) {
return appDataSource.getListFromDatabase();
}
retrunf someList;
}
}
Run Code Online (Sandbox Code Playgroud)
关键是 - 我希望该列表仅从DB加载一次.我想对这个功能进行单元测试.我是TDD中的noob,我能做的就是 - 为someList编写一个公共getter和setter,并在单元测试中使用它们.但它在概念上是错误的 - 我不希望类的客户端使用此成员变量directlty.
在这种情况下如何正确测试方法?
我曾尝试System.setProperty
在 main 方法中使用,没有任何问题,但是当我切换到TestNG
Selenium 学习的一部分时,我意识到我们无法System.setProperty
在类级别编写。它应该位于方法级别或位于static
块中。我只是想了解 Java 的什么特性迫使我们这样做。
public class NewTest {
public String baseUrl = "http://newtours.demoaut.com/";
static {
System.setProperty("webdriver.chrome.driver","D:\\paths\\chromedriver.exe");
}
WebDriver driver = new ChromeDriver();
@Test
public void f1() {
...}
}
Run Code Online (Sandbox Code Playgroud)
在静态块之外写入此内容会显示编译错误,例如“此行有多个标记,语法错误”
我能够实现带有@BeforeAll
注释的非静态设置方法。似乎只被调用一次,所以工作正常。我有点困惑,因为文档@BeforeAll
指出该方法必须是静态的。请解释。
@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml" })
@TestInstance(Lifecycle.PER_CLASS)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
public class MyTest
{
@BeforeAll
public void setup() throws Exception {...}
}
Run Code Online (Sandbox Code Playgroud) java ×6
junit ×5
unit-testing ×4
jestjs ×2
junit4 ×2
testing ×2
constructor ×1
javascript ×1
junit5 ×1
maven ×1
selenium ×1
spring-boot ×1
tdd ×1
testng ×1