Java 在测试之间传递 JUnit 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3144887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing JUnit data between tests
提问by orbfish
I just discovered when creating some CRUD tests that you can't set data in one test and have it read in another test (data is set back to its initialization between each test).
我刚刚在创建一些 CRUD 测试时发现,您无法在一个测试中设置数据并在另一个测试中读取它(数据在每个测试之间设置回其初始化)。
All I'm trying to do is (C)reate an object with one test, and (R)ead it with the next. Does JUnit have a way to do this, or is it ideologically coded such that tests are not allowed to depend on each other?
我要做的就是(C)用一个测试创建一个对象,然后(R)用下一个测试读取它。JUnit 是否有办法做到这一点,或者它是否按照意识形态进行编码,以至于不允许测试相互依赖?
采纳答案by Jason Wang
Well, for unit tests your aim should be to test the smallest isolated piece of code, usually method by method.
So testCreate()
is a test case and testRead()
is another. However, there is nothing that stops you from creating a testCreateAndRead()
to test the two functions together. But then if the test fails, which code unit does the test fail at? You don't know. Those kind of tests are more like integration test, which should be treated differently.
好吧,对于单元测试,您的目标应该是测试最小的隔离代码段,通常是一个方法一个方法。所以testCreate()
是一个测试用例,并testRead()
是另一回事。但是,没有什么可以阻止您创建一个testCreateAndRead()
来一起测试这两个函数。但是如果测试失败,测试失败的代码单元是什么?你不知道。这类测试更像是集成测试,应该区别对待。
If you really want to do it, you can create a static class variable to store the object created by testCreate()
, then use it in testRead()
.
如果你真的想这样做,你可以创建一个静态类变量来存储 . 创建的对象testCreate()
,然后在testRead()
.
As I have no idea what version of Junit you talking about, I just pick up the ancient one Junit 3.8:
由于不知道你说的是哪个版本的 Junit,我就挑了一个古老的 Junit 3.8:
Utterly ugly but works:
非常丑陋但有效:
public class Test extends TestCase{
static String stuff;
public void testCreate(){
stuff = "abc";
}
public void testRead(){
assertEquals(stuff, "abc");
}
}
回答by Andy Thomas
JUnit promotes independent tests. One option would be to put the two logical tests into one @Test method.
JUnit 提倡独立测试。一种选择是将两个逻辑测试放入一个 @Test 方法中。
TestNG was partly created to allow these kinds of dependencies among tests. It enforces local declarations of test dependencies -- it runs tests in a valid order, and does not run tests that depend on a failed test. See http://testng.org/doc/documentation-main.html#dependent-methodsfor examples.
TestNG 的部分创建是为了允许测试之间的这些类型的依赖关系。它强制执行测试依赖项的本地声明——它以有效的顺序运行测试,并且不运行依赖于失败测试的测试。有关示例,请参阅http://testng.org/doc/documentation-main.html#dependent-methods。
回答by emory
How much processing time do these tests take? If not a lot, then why sweat it. Sure you will create some object unnecessarily, but how much does this cost you?
这些测试需要多少处理时间?如果不是很多,那为什么要出汗呢?当然,您会创建一些不必要的对象,但这要花多少钱?
@Test
void testCreateObject() {
Object obj = unit.createObject();
}
@Test
void testReadObject() {
Object obj = null;
try {
obj = unit.createObject(); // this duplicates tests aleady done
} catch (Exception cause) {
assumeNoException(cause);
}
unit.readObject(obj);
}
回答by Franck
in this basic example, the variable is changed in the test A, and can be used in the test B
在这个基本示例中,变量在测试 A 中发生了变化,可以在测试 B 中使用
public class BasicTest extends ActivityInstrumentationTestCase2 {
public BasicTest() throws ClassNotFoundException {
super(TARGET_PACKAGE_ID, launcherActivityClass);
}
public static class MyClass {
public static String myvar = null;
public void set(String s) {
myvar = s;
}
public String get() {
return myvar;
}
}
private MyClass sharedVar;
@Override
protected void setUp() throws Exception {
sharedVar = new MyClass();
}
public void test_A() {
Log.d(S,"run A");
sharedVar.set("blah");
}
public void test_B() {
Log.d(S,"run B");
Log.i(S,"sharedVar is: " + sharedVar.get());
}
}
output result is:
输出结果为:
run A
运行A
run B
运行B
sharedVar is: blah
sharedVar 是:等等
回答by Long Nguyen
JUnit is independent test. But, If you have no ways, you can use "static" instance to store it.
JUnit 是独立的测试。但是,如果您没有办法,您可以使用“静态”实例来存储它。
static String storage;
@Test
public void method1() {
storage = "Hello"
}
@Test
public void method2() {
Assert.assertThat(something, is(storage));
}