简体   繁体   English

如何使用已设置测试用例的控制台应用程序进行自动化测试

[英]How to automation testing with a Console Application with already test cases set

I just complete a Java CONSOLE application for Student Management.我刚刚完成了一个用于学生管理的Java CONSOLE 应用程序

I received a test case set (pdf file contains lines follow according to the requirements of the application) build based on the standard program (from my lecturer) .我收到了基于标准程序(来自我的讲师)构建的测试用例集 pdf 文件包含根据应用程序要求遵循的行) You can overview what my app do and what is format of test casenter image description heree set in the attached image below.您可以概述我的应用程序的功能以及在下面的附图中设置的测试用例图像描述格式是什么。

The problem is that I want to use test cases for testing my app but instead of manually entering and matching line by line between Console IO and the pdf file => I want to write a program to automatically import and match the data between my jar/program to test cases .问题是我想使用测试用例来测试我的应用程序,而不是在控制台 IO 和 pdf 文件之间手动输入和逐行匹配=> 我想编写一个程序来自动导入和匹配我的 jar/ 之间的数据程序来测试用例

However, I'm not sure how and where to start.但是,我不确定如何以及从哪里开始。 I have tried with google but unit test/white testing is still the thing that takes up all of my search.我已经尝试过使用谷歌,但单元测试/白色测试仍然是我所有搜索的内容。 Hopefully in the process of continuing to try to search with google, someone will give me some suggestions or directions that will be useful to me.希望在继续尝试用google搜索的过程中,有人能给我一些对我有用的建议或方向。 Thanks very much.非常感谢。

[My Program] [我的节目]

[Test cases set] [测试用例集]

The way I'd do it is to decouple your application from the console so that you can use fake implementations for printing and reading from the console in your tests.我这样做的方法是将您的应用程序与控制台分离,以便您可以在测试中使用假实现从控制台打印和读取。 "Fake" is the technical term - you can look up "test doubles" to learn about those and other related ideas. “假”是一个技术术语——你可以查找“测试替身”来了解这些和其他相关的想法。 This idea is known as dependency injection, or the dependency inversion principle.这个想法被称为依赖注入,或依赖倒置原则。

The way we do this is to use interfaces.我们这样做的方式是使用接口。 Here's an example of an application that prints some items:下面是一个打印一些项目的应用程序示例:

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 is the thing responsible for the printing. OutputWriter是负责打印的东西。 It's just an interface, so the application doesn't know whether it writes to the console or somewhere else:它只是一个接口,所以应用程序不知道它是写入控制台还是其他地方:

public interface OutputWriter {
    void writeLine(String line);
}

For completeness, the Item class just holds some data:为了完整起见, Item类只包含一些数据:

public class Item {
    public Item(String name, Integer price) {
        this.name = name;
        this.price = price;
    }

    public final String name;
    public final Integer price;
}

I can then write a test using JUnit that checks that when I run this application, I get the output that I want.然后我可以使用JUnit编写一个测试,检查当我运行这个应用程序时,我得到了我想要的输出。 I do that by using an implementation of OutputWriter that just writes to a string.我通过使用只写入字符串的OutputWriter实现来做到这一点。 That way it's easy to check in the test:这样就很容易在测试中检查:

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
        );
    }
}

and FakeOutputWriter looks likeFakeOutputWriter看起来像

public class FakeOutputWriter implements OutputWriter {
    public String written = "";

    @Override
    public void writeLine(String line) {
        written += line;
        written += "\n";
    }
}

This gives me confidence that I'm writing the output correctly.这让我有信心正确地编写输出。 In main , though, I want to actually print to the console:不过,在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();
    }
}

and ConsoleOutputWriter does exactly that:ConsoleOutputWriter正是这样做的:

public class ConsoleOutputWriter implements OutputWriter{
    @Override
    public void writeLine(String line) {
        System.out.println(line);
    }
}

You could take the same approach for faking reading input.您可以采用相同的方法来伪造阅读输入。 Your interface would have a function that takes no arguments and reads a string:您的接口将有一个不带参数并读取字符串的函数:

interface InputReader {
    String readLine()
}

so in the tests you could fake that and in main , read using a Scanner or something.所以在测试中你可以伪造它,在main中,使用Scanner或其他东西阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM