是否可以在不更改任何代码的情况下对最初设计时未测试的代码进行单元测试?

发布于 2024-09-29 22:11:34 字数 1163 浏览 0 评论 0原文

人们是否普遍认为,除非将代码设置为要测试,否则无法测试代码?

一段假设的代码:

public void QueueOrder(SalesOrder order)
{
   if (order.Date < DateTime.Now-20)
      throw new Exception("Order is too old to be processed");
   ...  
}

有些人会考虑将其重构为:

protected DateTime MinOrderAge;
{
   return DateTime.Now-20;
}

public void QueueOrder(SalesOrder order)
{
   if (order.Date < MinOrderAge)
      throw new Exception("Order is too old to be processed");
   ...
}

注意:您可以想出更复杂的解决方案;涉及IClock接口和工厂。不影响我的提问。

更改上述代码的问题是代码已更改。代码已更改,而客户未要求更改。任何改变都需要召开会议和电话会议。所以我现在处于不测试任何东西更容易的地步。

如果我不愿意/无法进行更改:这是否会使我无法执行测试?

注意:上面的伪代码可能看起来像 C#,但这只是为了可读。这个问题与语言无关。

注意: 假设的代码片段、问题、重构需求以及重构都是假设的。如果您对我的代码示例不满意,您可以插入您自己的假设代码示例。

注意:以上假设代码是假设的。与任何代码的任何关系,无论是活着的还是死了的,都纯粹是巧合。

注意:代码是假设的,但任何答案都不是。这个问题不是主观的:因为我相信有一个答案。


更新:当然,这里的问题是我不能保证上面示例中的更改不会破坏任何内容。当然,我将一段代码重构为一个单独的方法,并且代码在逻辑上是相同的。

但我不能保证添加新的受保护方法不会偏移对象的虚拟方法表,并且如果此类位于 DLL 中,那么我刚刚引入了访问冲突。

Is it generally accepted that you cannot test code unless the code is setup to be tested?

A hypothetical bit of code:

public void QueueOrder(SalesOrder order)
{
   if (order.Date < DateTime.Now-20)
      throw new Exception("Order is too old to be processed");
   ...  
}

Some would consider refactoring it into:

protected DateTime MinOrderAge;
{
   return DateTime.Now-20;
}

public void QueueOrder(SalesOrder order)
{
   if (order.Date < MinOrderAge)
      throw new Exception("Order is too old to be processed");
   ...
}

Note: You can come up with even more complicated solutions; involving an IClock interface and factory. It doesn't affect my question.

The issue with changing the above code is that the code has changed. The code has changed without the customer asking for it to be changed. And any change requires meetings and conference calls. And so i'm at the point where it's easier not to test anything.

If i'm not willing/able to make changes: does it make me not able to perform testing?

Note: The above pseudo-code might look like C#, but that's only so it's readable. The question is language agnostic.

Note: The hypothetical code snippet, problem, need for refactoring, and refactoring are hypothetical. You can insert your own hypothetical code sample if you take umbrage with mine.

Note: The above hypothetical code is hypothetical. Any relation to any code, either living or dead, is purely coincidental.

Note: The code is hypothetical, but any answers are not. The question is not subjective: as i believe there is an answer.


Update: The problem here, of course, is that i cannot guarantee that change in the above example didn't break anything. Sure i refactored one piece of code out to a separate method, and the code is logically identical.

But i cannot guarantee that adding a new protected method didn't offset the Virtual Method Table of the object, and if this class is in a DLL then i've just introduced an access violation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

日裸衫吸 2024-10-06 22:11:34

答案是肯定的,需要更改一些代码才能使其可测试。

但可能有很多代码无需更改即可进行测试。我会首先专注于为这些内容编写测试,然后当其他客户需求让您有机会以可测试的方式重构它时,为其余内容编写测试。

The answer is yes, some code will need to change to make it testable.

But there is likely lots of code that can be tested without having to change it. I would focus on writing tests for that stuff first, then writing tests for the rest when other customer requirements give you the opportunity to refactor it in a testable way.

我早已燃尽 2024-10-06 22:11:34

代码可以从一开始就编写为可测试的。如果从一开始就没有考虑到可测试性,您仍然可以测试它,只是可能会遇到一些困难。

在假设的代码中,您可以通过创建一个日期很早的 SalesOrder 来测试原始代码,或者您可以模拟 DateTime.Now。按照您所展示的方式重构代码更适合测试,但这并不是绝对必要的。

Code can be written from the start to be testable. If it is not written from the start with testability in mind, you can still test it, you may just run into some difficulties.

In your hypothetical code, you could test the original code by creating a SalesOrder with a date far in the past, or you could mock out DateTime.Now. Having the code refactored as you showed is nicer for testing, but it isn't absolutely necessary.

森林散布 2024-10-06 22:11:34

如果您的代码不是为测试而设计的,那么测试它就会更加困难。在您的示例中,您必须重写 DateTime.Now 方法,这可能不是一件容易的任务。

我认为向代码添加测试几乎没有什么价值,或者不允许更改现有代码,那么您不应该这样做。

但是,如果您相信 TDD,那么您应该编写带有测试的新代码。

If your code is not designed to be tested then it is more difficult to test it. In your example you would have to override the DateTime.Now Method which is propably no easy task.

I you think it adds little value to add tests to your code or the changing of existing code is not allowed then you should not do it.

However if you belief in TDD then you should write new code with tests.

与之呼应 2024-10-06 22:11:34

您可以使用模拟对象框架对原始示例进行单元测试。在本例中,我将多次模拟 SalesOrder 对象,每次配置不同的日期值,然后进行测试。这可以避免更改任何附带的代码,并允许您验证相关算法,确保订单日期距离过去不远。

为了更好地全面了解您正在处理的依赖项以及您可以使用的语言功能,我建议 有效使用旧代码

You can unit test your original example using a Mock object framework. In this case I would mock the SalesOrder object several times, configuring a different Date value each time, and test. This avoids changing any code that ships and allows you to validate the algorithm in question that the order date is not too far in the past.

For a better overall view of what's possible given the dependencies you're dealing with, and the language features you have at your disposal, I recommend Working Effective with Legacy Code.

枕梦 2024-10-06 22:11:34

这在某些动态语言中很容易实现。例如,我可以挂钩 import/using 语句并将实际依赖项替换为存根依赖项,即使 SUT(被测系统)将其用作隐式依赖项。或者我可以重新定义这些符号(类、方法、函数等)。我并不是说这是要走的路。事情应该重构,但编写一些特征测试更容易。

This is easy to accomplish in some dynamic languages. For example I can hook inside the import/using statements and replace an actual dependency with a stub one, even if the SUT (System Under Test) uses it as an implicit dependency. Or I can redefine those symbols (classes, methods, functions, etc.). I'm not saying this is the way to go. Things should be refactored, but it is easier to write some characterization tests.

极度宠爱 2024-10-06 22:11:34

此类代码的问题始终是,它正在创建并依赖于许多静态类、框架类型等...

为所有这些对象“注入”假货的一个非常好的解决方案是 Typemock Isolator(它是商业性的,但值得每一分钱)。所以,是的,您当然可以测试遗留代码,这些代码是在没有考虑可测试性的情况下编写的。我已经用 Typemock 在一个大项目上完成了它,并取得了非常好的结果。

除了 Typemock 之外,您还可以使用免费的 MS Moles 框架,它的功能基本相同。只是它的 API 相当不直观,并且更难学习和使用。

HTH。
托马斯

The problem with this sort of code is always, that it's creating and depending on a lot of static classes, framework types, etc. etc. ...

A very good solution to 'inject' fakes for all these objects is Typemock Isolator (which is commercial, but worth every penny). So yes, you certainly can test legacy code, which was written without testability in mind. I've done it on a big project with Typemock and had very good results.

Alternatively to Typemock, you may use the free MS Moles framework, which does basically the same. It's only that it has a quite unintuitive API and is much harder to learn and use.

HTH.
Thomas

月光色 2024-10-06 22:11:34

Mockito + PowerMock for Mockito。

您将能够测试几乎所有内容,而无需大幅更改代码。但是需要一些设置器来注入模拟。

Mockito + PowerMock for Mockito.

You'll be able to test almost everything without dramatically changing your code. But some setters will be needed to inject the mocks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文