我有一些常见的设置代码,我已经考虑到标记的方法@Before.但是,并非所有这些代码都必须针对每个测试运行.有没有办法对其进行标记,以便该@Before方法仅在某些测试之前运行?
基本上,我有一个拆解方法,我想要登录到刚刚运行测试的控制台.如何获得该字符串?
我可以获取类名,但我想要刚刚执行的实际方法.
public class TestSomething {
@AfterMethod
public void tearDown() {
System.out.println("The test that just ran was: " + getTestThatJustRanMethodName());
}
@Test
public void testCase() {
assertTrue(1 == 1);
}
}
Run Code Online (Sandbox Code Playgroud)
...应该输出到屏幕:"刚刚运行的测试是:testCase"
但是,我不知道getTestThatJustRanMethodName应该具有的魔力.
在JUnit测试用例中,注释的字段@Rule必须是公共的.它打破了常见的Java编码约定(所有类成员变量都不应该是公共的).为什么JUnit需要这个?
文档@Rule:https://github.com/junit-team/junit/blob/master/src/main/java/org/junit/Rule.java
我可以像在JUnit中一样检索当前运行的测试名称(使用getName()或规则)吗?
@Test
public void fooBar(){
System.out.println(magic()); //should print "fooBar"
}
Run Code Online (Sandbox Code Playgroud)
PS我不想使用一些基于堆栈跟踪的自编工具.
如何在JUnit 5中获取测试方法的名称?
我想在执行JUnit测试时做一些日志记录.在JUnit 3.x中,无论实例化测试用例如何,总是很容易获得当前运行的测试用例的名称:
public void testFoo() throws Exception() {
String testName = this.getName();
// [...] do some stuff
}
Run Code Online (Sandbox Code Playgroud)
在JUnit 4中,事情似乎并不那么容易.有谁知道解决这个问题?是否有任何选项可以反映到当前的Runner实例中?
我的公司最近转向了maven,过渡非常顺利,但有一个令我烦恼.所以早些时候,在pre-maven时代,你可以看到哪个测试是当前正在运行的类(如果你在一个类中进行了40次单元测试,那么第二次测试失败就会看到它).现在它看起来像这样:它显示测试类的名称,就是这样.您必须等到所有测试都在课堂上完成才能真正看到结果(您可以停止测试,它会显示您停止的点的进度,但在实际停止之前您没有看到任何内容).在某些集成测试中,这非常烦人且耗时.
如果有人知道解决方案,我将不胜感激.提前致谢.
junit 4.10如何在测试开始运行之前打印测试用例名称..
所以我想在这里打印"sampleTest".
我怎么在junit 4.10做到这一点?先感谢您
class1:
TestSuite all = new TestSuite();
all.addTestSuite(class2);
all.run(result);
class2:
public class profileTest extends TestCase()
{
//I want to print the test cases name before the test actually starts executing
@Test
public void sampleTest1(){
//Some code here.
}
@Test
public void sampleTest2(){
//some more code here.
}
}
Run Code Online (Sandbox Code Playgroud) 我想获得当前正在执行的测试方法,@Before以便我可以在当前正在执行的方法上应用注释.
public class TestCaseExample {
@Before
public void setUp() {
// get current method here.
}
@Test
@MyAnnotation("id")
public void someTest {
// code
}
}
Run Code Online (Sandbox Code Playgroud) 我想在@Before方法中获取当前正在执行的TestCase方法的名称.例
public class SampleTest()
{
@Before
public void setUp()
{
//get name of method here
}
@Test
public void exampleTest()
{
//Some code here.
}
}
Run Code Online (Sandbox Code Playgroud) java ×10
junit ×6
unit-testing ×3
reflection ×2
testing ×2
testng ×2
junit-rule ×1
junit4 ×1
junit5 ×1
maven ×1
methods ×1
progress ×1
public ×1