如何对创建新流程的代码进行单元测试?

时间:2023-01-13 10:07:52

How can I test the following method?

我该如何测试以下方法?

It is a method on a concrete class implementation of an interface.

它是接口的具体类实现的方法。

I have wrapped the Process class with an interface that only exposes the methods and properties I need. The ProcessWrapper class is the concrete implementation of this interface.

我用一个只暴露我需要的方法和属性的接口包装了Process类。 ProcessWrapper类是此接口的具体实现。

    public void Initiate(IEnumerable<Cow> cows)
    {
        foreach (Cow c in cows)
        {
            c.Process = new ProcessWrapper(c);
            c.Process.Start();
            count++;
        }
    }

3 个解决方案

#1


There are two ways to get around this. The first is to use dependency injection. You could inject a factory and have Initiate call the create method to get the kind of ProcessWrapper you need for your test.

有两种方法可以解决这个问题。第一种是使用依赖注入。您可以注入一个工厂并让Initiate调用create方法来获得测试所需的ProcessWrapper。

The other solution is to use a mocking framework such as TypeMock, that will let you work around this. TypeMock basically allows you to mock anything, so you could use it to provide a mock object instead of the actual ProcessWrapper instances.

另一个解决方案是使用一个模拟框架,如TypeMock,这将让你解决这个问题。 TypeMock基本上允许您模拟任何东西,因此您可以使用它来提供模拟对象而不是实际的ProcessWrapper实例。

#2


What does your process do? Is there any way you could check that it is doing what it's supposed to do? For example, it might write to a file or a database table. Or it might expose an API (IPC, web-service, etc.) that you could try calling with test data.

你的过程有什么作用?有什么方法可以检查它是否正在做它应该做的事情?例如,它可能写入文件或数据库表。或者它可能会暴露您可以尝试使用测试数据调用的API(IPC,Web服务等)。

From a TDD perspective, it might make make sense to plug in a "mock/test process" that performs some action that you can easily check. (This may require code changes to allow your test code to inject something.) This way, you're only testing your invocation code, and not-necessarily testing an actual business process. You could then have different unit tests to test your business process.

从TDD的角度来看,插入一个“模拟/测试过程”来执行一些您可以轻松检查的操作可能是有意义的。 (这可能需要更改代码以允许您的测试代码注入一些东西。)这样,您只测试调用代码,而不一定测试实际的业务流程。然后,您可以使用不同的单元测试来测试业务流程。

#3


I'm not familiar with C# (I prefer mine without the hash), but you need some sort of interface to the process (IPC or whatever is the most convenient method) so you can send it test requests and get results back. At the simplest level, you would just send a message to the process and receive the result. Or you could have more granularity and send more specific commands from your test harness. It depends on how you have set up your unit testing environment, more precisely how you send the test commands, how you receive them and how you report the results.

我不熟悉C#(我更喜欢没有哈希),但你需要某种程序的接口(IPC或任何最方便的方法),这样你就可以发送测试请求并获得结果。在最简单的级别,您只需向进程发送一条消息并接收结果。或者您可以拥有更多粒度并从测试工具中发送更具体的命令。这取决于您如何设置单元测试环境,更准确地说是如何发送测试命令,如何接收它们以及如何报告结果。

I would personally have a test object inside the process that simply receives, runs & reports the unit test results and have the test code inside that object.

我个人会在流程中有一个测试对象,只需接收,运行和报告单元测试结果,并在该对象中包含测试代码。

#1


There are two ways to get around this. The first is to use dependency injection. You could inject a factory and have Initiate call the create method to get the kind of ProcessWrapper you need for your test.

有两种方法可以解决这个问题。第一种是使用依赖注入。您可以注入一个工厂并让Initiate调用create方法来获得测试所需的ProcessWrapper。

The other solution is to use a mocking framework such as TypeMock, that will let you work around this. TypeMock basically allows you to mock anything, so you could use it to provide a mock object instead of the actual ProcessWrapper instances.

另一个解决方案是使用一个模拟框架,如TypeMock,这将让你解决这个问题。 TypeMock基本上允许您模拟任何东西,因此您可以使用它来提供模拟对象而不是实际的ProcessWrapper实例。

#2


What does your process do? Is there any way you could check that it is doing what it's supposed to do? For example, it might write to a file or a database table. Or it might expose an API (IPC, web-service, etc.) that you could try calling with test data.

你的过程有什么作用?有什么方法可以检查它是否正在做它应该做的事情?例如,它可能写入文件或数据库表。或者它可能会暴露您可以尝试使用测试数据调用的API(IPC,Web服务等)。

From a TDD perspective, it might make make sense to plug in a "mock/test process" that performs some action that you can easily check. (This may require code changes to allow your test code to inject something.) This way, you're only testing your invocation code, and not-necessarily testing an actual business process. You could then have different unit tests to test your business process.

从TDD的角度来看,插入一个“模拟/测试过程”来执行一些您可以轻松检查的操作可能是有意义的。 (这可能需要更改代码以允许您的测试代码注入一些东西。)这样,您只测试调用代码,而不一定测试实际的业务流程。然后,您可以使用不同的单元测试来测试业务流程。

#3


I'm not familiar with C# (I prefer mine without the hash), but you need some sort of interface to the process (IPC or whatever is the most convenient method) so you can send it test requests and get results back. At the simplest level, you would just send a message to the process and receive the result. Or you could have more granularity and send more specific commands from your test harness. It depends on how you have set up your unit testing environment, more precisely how you send the test commands, how you receive them and how you report the results.

我不熟悉C#(我更喜欢没有哈希),但你需要某种程序的接口(IPC或任何最方便的方法),这样你就可以发送测试请求并获得结果。在最简单的级别,您只需向进程发送一条消息并接收结果。或者您可以拥有更多粒度并从测试工具中发送更具体的命令。这取决于您如何设置单元测试环境,更准确地说是如何发送测试命令,如何接收它们以及如何报告结果。

I would personally have a test object inside the process that simply receives, runs & reports the unit test results and have the test code inside that object.

我个人会在流程中有一个测试对象,只需接收,运行和报告单元测试结果,并在该对象中包含测试代码。