![](/img/trans.png)
[英]Appium : how to check device name using automation test cases in java
[英]How to automation testing with a Console Application with already test cases set
我刚刚完成了一个用于学生管理的Java CONSOLE 应用程序。
我收到了基于标准程序(来自我的讲师)构建的测试用例集( pdf 文件包含根据应用程序要求遵循的行) 。 您可以概述我的应用程序的功能以及在下面的附图中设置的测试用例图像描述格式是什么。
问题是我想使用测试用例来测试我的应用程序,而不是在控制台 IO 和 pdf 文件之间手动输入和逐行匹配=> 我想编写一个程序来自动导入和匹配我的 jar/ 之间的数据程序来测试用例。
但是,我不确定如何以及从哪里开始。 我已经尝试过使用谷歌,但单元测试/白色测试仍然是我所有搜索的内容。 希望在继续尝试用google搜索的过程中,有人能给我一些对我有用的建议或方向。 非常感谢。
我这样做的方法是将您的应用程序与控制台分离,以便您可以在测试中使用假实现从控制台打印和读取。 “假”是一个技术术语——你可以查找“测试替身”来了解这些和其他相关的想法。 这个想法被称为依赖注入,或依赖倒置原则。
我们这样做的方式是使用接口。 下面是一个打印一些项目的应用程序示例:
import java.util.List;
public class ItemPrinterApplication {
public ItemPrinterApplication(OutputWriter outputWriter, List<Item> items) {
this.outputWriter = outputWriter;
this.items = items;
}
public void run() {
outputWriter.writeLine("Name, Price");
items.forEach(item -> outputWriter.writeLine(item.name + ", " + item.price));
}
private OutputWriter outputWriter;
private List<Item> items;
}
OutputWriter
是负责打印的东西。 它只是一个接口,所以应用程序不知道它是写入控制台还是其他地方:
public interface OutputWriter {
void writeLine(String line);
}
为了完整起见, Item
类只包含一些数据:
public class Item {
public Item(String name, Integer price) {
this.name = name;
this.price = price;
}
public final String name;
public final Integer price;
}
然后我可以使用JUnit编写一个测试,检查当我运行这个应用程序时,我得到了我想要的输出。 我通过使用只写入字符串的OutputWriter
实现来做到这一点。 这样就很容易在测试中检查:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
public class ItemPrinterTest {
@Test
public void itPrintsAListOfItems() {
List<Item> items =
List.of(
new Item("Apple", 50),
new Item("Carrot", 25),
new Item("Milk", 120)
);
FakeOutputWriter fakeOutputWriter = new FakeOutputWriter();
ItemPrinterApplication app = new ItemPrinterApplication(fakeOutputWriter, items);
app.run();
Assertions.assertEquals(
"Name, Price\n" +
"Apple, 50\n" +
"Carrot, 25\n" +
"Milk, 120\n",
fakeOutputWriter.written
);
}
}
和FakeOutputWriter
看起来像
public class FakeOutputWriter implements OutputWriter {
public String written = "";
@Override
public void writeLine(String line) {
written += line;
written += "\n";
}
}
这让我有信心正确地编写输出。 不过,在main
中,我想实际打印到控制台:
import java.util.List;
public class Main {
public static void main(String[] args) {
OutputWriter outputWriter = new ConsoleOutputWriter();
List<Item> items =
List.of(
new Item("Apple", 50),
new Item("Carrot", 25),
new Item("Milk", 120)
);
new ItemPrinterApplication(outputWriter, items).run();
}
}
而ConsoleOutputWriter
正是这样做的:
public class ConsoleOutputWriter implements OutputWriter{
@Override
public void writeLine(String line) {
System.out.println(line);
}
}
您可以采用相同的方法来伪造阅读输入。 您的接口将有一个不带参数并读取字符串的函数:
interface InputReader {
String readLine()
}
所以在测试中你可以伪造它,在main
中,使用Scanner
或其他东西阅读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.