Codewars Python TDD offlin

2024-07-05 10:08:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用我熟悉的开发环境离线做codewarspythonkatas。但是,提供的测试使用与Python的Unittest完全不同的语法。我在任何地方都找不到测试框架的源代码。在

我试过codewars客户端npm包(https://github.com/shime/codewars),但它让我很困惑。我也看过codewarsclirunner,但这看起来更难摸索,而且涉及到Docker。在

这很令人沮丧,因为我真的只想练习一些基本的编码,但是我最终不得不尝试理解json和依赖关系以及包管理,只是为了让一个基本的TDD环境启动并运行。在

有谁能建议一下如何在本地使用pythonkatas中提供的测试?示例如下:

test.describe("Basic tests")
test.it("A resistor under 1000 ohms and with only three bands")   
test.assert_equals(decode_resistor_colors("yellow violet black"), "47 ohms, 20%")
test.it("A resistor between 1000 and 999999 ohms, with a gold fourth band")   
test.assert_equals(decode_resistor_colors("yellow violet red gold"), "4.7k ohms, 5%")
test.it("A resistor of 1000000 ohms or above, with a silver fourth band")   
test.assert_equals(decode_resistor_colors("brown black green silver"), "1M ohms, 10%")

Tags: andtest环境withitassertcodewarsblack
3条回答

我只需将测试用例转换为代码中的打印语句,并注释掉其余的语句。然后目测比较答案。在

见下文:

# test.describe("Basic tests")
# test.it("A resistor under 1000 ohms and with only three bands")   
print(decode_resistor_colors("yellow violet black")) # "47 ohms, 20%"

# test.it("A resistor between 1000 and 999999 ohms, with a gold fourth band")   
print(decode_resistor_colors("yellow violet red gold")) #  "4.7k ohms, 5%"

# test.it("A resistor of 1000000 ohms or above, with a silver fourth band")   
print(decode_resistor_colors("brown black green silver")) # "1M ohms, 10%"

我建议使用python-code-kata 您可以使用当前示例或搜索其他代码kata。 原理和“codewars”是一样的——它使用测试来检查你的答案。主要的好处是你可以在你的机器上设置它并离线使用它。(据我所知,这对你很重要)

我使用自己的类来模拟Codewars测试行为。在

你可以在这里看到我的例子: Codewars Tests

相关问题 更多 >