[英]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: 单元测试的一个好的经验法则是:
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.