TDD 修改我的测试以使我的代码通过

huangapple go评论75阅读模式
英文:

TDD modifying my test to make my code pass

问题

I can help you with the translation. Here is the translated content:

我正在学习测试驱动开发,我有些困难,似乎我的大脑更想构建解决方案算法,而不是构建我的算法所需的测试。我几乎无法形式化单元测试,我来回修改我的测试。

我达到一个点,在代码中进行了一些调整,然后尝试修改我的测试以使它们通过!应该是相反的顺序。

最初我有这个测试代码:

    def test_is_zero_in_range(self):
        self.assertRaises(Exception, self._provServerPrepare.is_in_allow_range, 0)

所以我编写了所需的代码:

    def is_in_allow_range(self, value: str) -> int:
        value = int(value)
        if value in MULTIPLY_PROV_RANGE:
            return value
        raise Exception('Value out of range')

由于我在使用Flask,我更改了我的代码以获得一个带有消息和错误代码的返回,而不是引发异常:

    def is_in_allow_range(self, value: str) -> int:
        value = int(value)
        if value in MULTIPLY_PROV_RANGE:
            return value
        return {"message": f"Value out of range"}, 400

在这种情况下,我必须更改我的测试以使其工作,我不太确定,但我感觉我在摆弄一些东西,这对我来说似乎不太对。

有人能帮助我吗,或者有任何资源可以让我阅读/观看吗?

谢谢。

英文:

I'm learning Test Driven Development, i'm struggling a little bit, seems like my brain wants to build solution algorithm, and not the needed test to build my algorithm. I can barely formalize unit test, and i go back and forth to change my tests.
I come to a point where i make some adjustement in my code, and then i'm trying to modify my tests to make them pass! It should be the other way around.

i had initially this piece of code for test

    def test_is_zero_in_range(self):
        self.assertRaises(Exception, self._provServerPrepare.is_in_allow_range, 0)

so i write it up the code needed

    def is_in_allow_range(self, value: str) -> int:
        value = int(value)
        if value in MULTIPLY_PROV_RANGE:
            return value
        raise Exception('Value out of range')

as i'm working with flask, i changed my code to get a return with a message and an error code, rather than a raise exception

    def is_in_allow_range(self, value: str) -> int:
        value = int(value)
        if value in MULTIPLY_PROV_RANGE:
            return value
        return {"message": f"Value out of range"}, 400 

In this situation i have to change my tests to make it work, i'm not sure, but i feel like i'm messing with something, this doesn't seem right to me to do it that way.

Does anyone can help me on this, or have any resources for me to read/watch?

Thx

答案1

得分: 1

TDD 不是唯一的工具。

> 在这种情况下,我必须更改我的测试以使其工作,我不确定,但我觉得我在捣鼓一些东西,这种方式对我来说似乎不对。

您可能错过了“spiking”的技巧。

总之,当我们对自己足够了解问题以产生解决方案不够自信时,我们首先进行一些实验以改善我们的理解,然后根据新的理解开始TDD过程。

我们并不一定试图解决整个问题,而是要对它有足够的感觉,以便我们有信心能够解决整个问题。

“Spiking”包括一个重要的限制:如果您想长期受益于测试优先方法,那么在“spike”结束时,您应该放弃在“spike”期间所做的工作并“从零开始”。

“Spiking”作为一种实践,不是一个“TDD”实践。这一实践(以及这一实践的标签)是从极限编程社区继承而来的。


也就是说,当您发现测试描述的行为是错误的,或者测试和受测对象之间的接口不足够时,是的,很不幸,测试将会发生变化。

有时这将意味着修改旧测试;有时这将意味着废弃旧测试,并用新的东西替代它们。


有时,您可以使用“信息隐藏”作为对未来变更的一种对策 - 即,如果您早期意识到设计中的特定元素是不稳定的,那么您可能会做一些额外的工作,以减少如果不稳定设计确实破裂时造成的未来损害。

英文:

TDD is not the only tool in the kit.

> In this situation i have to change my tests to make it work, i'm not sure, but i feel like i'm messing with something, this doesn't seem right to me to do it that way.

You may be missing the technique of spiking.

In summary: when we aren't confident enough that we understand a problem well enough to produce a solution, we first perform some experiments to improve our understanding, and then from that new understanding we start the TDD process.

We're not necessarily trying to solve the whole problem, but instead to get enough of a sense for it that we are confident that an approach will be able to solve the whole problem.

Spiking includes one important constraint: if you want to reap the long term benefits of a test first approach, then you discard the work you do during the spike and "start from scratch" when the spike is over.

Spiking, as a practice, isn't a "TDD" practice as such. The practice (and the label for the practice) were inherited by the TDD community from the Extreme Programming community.


That said - when discover that a behavior described by your tests is wrong, or that the interface(s) between the test and the subject are not adequate, then yeah - bad luck, there are going to be changes to the tests.

Sometimes that is going to mean modifying the old tests; sometimes that will mean retiring the old tests and replacing them with something new.


You can sometimes use information hiding as a counter-measure against future changes - ie if you recognize early that a particular element in your design is unstable, then you might do a bit of extra work to reduce future damage if the unstable design does in fact rupture.

答案2

得分: 1

代码部分不需要翻译。以下是翻译好的内容:

"在你编写测试后,经常会发现需要更改函数的返回值(或引发的异常)。这很正常。采用TDD的方式来处理这个问题是首先修改测试以反映新的正确返回值。看到测试失败,然后更新代码以使其通过。

因此,在你的示例中,当你切换到 Flask,或者当你意识到需要返回消息和错误代码而不是引发异常时,你首先会修改 test_is_zero_in_range,要求消息和错误代码,看到它失败,然后更新 is_in_allow_range 使其通过。"首先测试"。

顺便说一下,以下是一些可能更合适的测试和函数的替代名称:

test_zero_is_out_of_rangetest_zero_should_be_out_of_range
is_in_allowed_range

这个测试名称更加自解释,因为它陈述了测试的意图。"

英文:

It happens all the time that after you write a test, you find that you need to change the return value from a function (or the exception that it raises). That's normal. The TDD way to address this is to change the test first to reflect the new correct return value. See that test fail, and then update your code to make it pass.

So, in your example, when you switched to flask, or when you realized that you needed to return a message and error code rather than raise an exception, you would first modify test_is_zero_in_range to require the message and error code, see it fail, and then update is_in_allow_range to make it pass. "Test first."

By the way, here are some alternative names for your test and your function, which you might like better:

test_zero_is_out_of_range  or  test_zero_should_be_out_of_range<br/>
is_in_allowed_range

This test name is more self-explanatory, since it states the intent of the test.

答案3

得分: 0

你正在发现你的实际需求并朝着那个方向发展,这是TDD的主要目标和好处之一。改变主意没有错。

如果你有十几个测试在调用,然后需要改变调用方式,你有两个选项:

  • 使用像PyCharm这样支持“更改签名”重构的IDE。
  • 在测试端提取一个使调用的辅助方法。然后你只需更改一个地方。这种辅助方法通常会从测试代码移到生产代码。

在使用TDD时还要注意的一件事情:不要从边缘情况开始。相反,捕捉你想要的内容的简化子集。在Industrial Logic,我们称之为“Essence First”。抛出异常可能不是你想要进行TDD的行为的本质,所以不要从那里开始。相反,从“快乐路径”的最小表达开始。

英文:

You're discovering your actual requirements and evolving your way there, which is one of the primary goals and benefits of TDD. There is nothing wrong with changing your mind.

If you have a dozen tests making a call, and then you need to change how that call works, you have two options:

  • Use an IDE like PyCharm that supports the Change Signature refactoring.
  • Extract a helper method on the test side that makes the call. Then you only have to change one place. Such helpers often move from test code to production code.

Another thing to be aware of with TDD: Don't start with an edge case. Instead, capture a stripped-down subset of what you want. At Industrial Logic, we call this Essence First. Throwing an exception is likely not the essence of the behavior you want to TDD, so don't start there. Instead, start with a minimal expression of the "happy path."

huangapple
  • 本文由 发表于 2023年4月1日 00:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定