简体   繁体   English

单元测试main.py与其他模块

[英]unit test main.py with other modules

This is more a conceptual question. 这是一个概念性问题。 I am unit testing a script I wrote that has multiple modules. 我是单元测试我写的有多个模块的脚本。 I have a main.py and a formatting.py. 我有一个main.py和一个formatting.py。 My coverage is at 100% for formatting but my main.py is at 30%. 格式化我的覆盖率为100%,但我的main.py为30%。 In my main, I just call all of the functions inside formatting. 在我的主要内容中,我只是调用格式化中的所有函数。 Do i need to just test them again in main directly? 我是否需要直接在主要测试它们? This seems like a waste of time? 这似乎浪费时间? Maybe I am not understanding correctly. 也许我没有正确理解。 Thanks in advance 提前致谢

Usually the trick is to test what external function does by itself and not other functions that are called and are already tested. 通常的技巧是测试外部函数本身的功能,而不是其他被调用且已经过测试的函数。 This is not a problem if internal functions are actually called, just avoid testing them again. 如果实际调用内部函数,这不是问题,只是避免再次测试它们。

If you want to avoid calling internal functions though, you may try dependecy injection or mocking. 如果你想避免调用内部函数,你可以尝试依赖注入或模拟。

If your external function is simple enough, you may forgo testing it, 100% coverage is not a rule. 如果您的外部功能足够简单,您可以放弃测试,100%覆盖率不是常规。

A good rule of thumb for unit testing is to: 单元测试的一个好的经验法则是:

  • separate your code from the part that does logic and the part that does input/output 将代码与执行逻辑的部分和执行输入/输出的部分分开
  • unit test the part that does logic 单元测试做逻辑的部分
  • not unit test the part that does i/o 不进行单元测试i / o的部分

But how will you know the program works then? 但是你怎么知道这个程序呢? Well, how about some integration tests? 那么,一些集成测试怎么样? For example, if you're writing a command line script, an integration test might run it the whole script with some inputs and check if the script did the right thing, without even considering how it is structured. 例如,如果您正在编写命令行脚本,则集成测试可能会使用一些输入运行整个脚本,并检查脚本是否做了正确的事情,甚至没有考虑它的结构。

Depending on your needs and the size of the script, you might decide to have unit tests, integration tests or both. 根据您的需要和脚本的大小,您可能决定进行单元测试,集成测试或两者兼而有之。

The fact that internal pieces are tested allows you to capitalize on it by reducing the number of the tests required for the new, higher level method. 内部部件经过测试的事实允许您通过减少新的更高级别方法所需的测试数量来利用它。

There always will be some redundancy between those, but decent and straightforward seams would allow you to have a very few tests for the new level. 这些之间总会有一些冗余,但是体面和直接的接缝会让你对新级别进行很少的测试。

The guys in the other answers are talking about considering those tests as integrational, but I would claim that you also would need some tests in isolation for the upper function itself with all already tested dependencies being mocked and excluded. 其他答案中的人都在谈论将这些测试视为整合,但我会声称你还需要一些独立的测试来处理上层函数本身,所有已测试的依赖项都被模拟和排除。 It's not always necessary, but please be aware that you end up with a mixed test otherwise, since there's a new functionality on the top level. 这并不总是必要的,但请注意,否则最终会进行混合测试,因为顶层有新功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试main.py - Unit testing main.py 如何将fixed_seed从main.py模块转移到其他模块? - How can I transfer a fixed_seed from main.py module to other modules? 我可以在其他文件中使用 main.py 中的变量吗? - Can I use variables from main.py in other files? 当您只是从其他地方(例如 main.py)导入一段 test.py 时,为什么要运行 python 文件(例如 test.py)的其他代码行? - Why do you run other lines of codes of a python file(say test.py) when you are just importing a piece of test.py from somewhere else (say main.py)? main.py 中的 .env 文件 - .env file in main.py 运行 python main.py 或运行 test_main.py 时未找到模块名称错误 - Module name not found error either while running python main.py or running test_main.py 如何使用内置的“ test / main.py”主函数创建python包? - How do you create a python package with a built in “test/main.py” main function? 如何将数据从 main.qml 传递到 main.py,然后将其发送到其他 qml 文件 - How do you pass data from main.qml to main.py then send it to other qml files Python 尝试 package 我的 main.py 与模块但总是相同的错误: ModulenotfoundError - Python Try to package my main.py with modules but always same error : ModulenotfoundError GAE:如何在我的应用引擎 main.py 中包含其他类 - GAE: How to include other classes in my app engine main.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM