pytestest ting主

发布于 2025-02-07 17:41:52 字数 981 浏览 1 评论 0原文

考虑一个简单的程序,例如:

  • main.py
  • reader.py
  • test_reader.py,

让我们假设reader.py是由test_reader.py进行的100%覆盖范围测试。

测试一个看起来像这样的主的测试将如何进行:

from Reader import myClass
import argparse
if __name__ == '__main__':
    # argparsing eluded
    parser = argparse.ArgumentParser(description='my descrition')
    parser.add_argument('myarg1', nargs='?',
                default = 1)
    parser.add_argument('myarg2', nargs='?',
                default = 1)
    args = parser.parse_args()
    myClass(args.myarg1, args.myarg2)

如何处理? 我的第一个论点是,该算法是如此简单,因此不需要测试。但是,我不确定您是否需要测试它是否有原因。

我知道嘲笑argparse需要一些嘲笑,如这个问题所述: pytest with agrparse:如何提示用户进行确认? 但是,实际嘲笑简单的东西(所有实际操作都在其他模块中)是否值得努力?

Considering a simple program like:

  • main.py
  • Reader.py
  • test_Reader.py

Let's assume Reader.py is 100% coverage-tested by test_Reader.py.

How would one go with testing a main that would look like this:

from Reader import myClass
import argparse
if __name__ == '__main__':
    # argparsing eluded
    parser = argparse.ArgumentParser(description='my descrition')
    parser.add_argument('myarg1', nargs='?',
                default = 1)
    parser.add_argument('myarg2', nargs='?',
                default = 1)
    args = parser.parse_args()
    myClass(args.myarg1, args.myarg2)

How would one go with that?
My first argument was that the algorithm is so simple it does not require to be tested. However, I am not sure if there is a reason why you would need to test that.

I know mocking argparse requires some mocking as explained in this question : Pytest with argparse: how to test user is prompted for confirmation?
But is it worth the effort to actually mock something that simple (all the actual operations are in the other module)?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文