开发者

Importing module-under-test per test rather than module level imports?

开发者 https://www.devze.com 2023-02-16 09:52 出处:网络
I\'ve recently come across some unit testing code that imports the modules to test in the function to test, rather than a module level import.

I've recently come across some unit testing code that imports the modules to test in the function to test, rather than a module level import.

Then, after Googling it, I found that Pylons/Pyramid best practices reason that "import failures...should never prevent those tests from being run."

Should that be standard practice?

I find it a bit开发者_Go百科 ugly, plus, their class example looks like slight over-engineering.


If you import all the modules tested at the top of the file, instead of in the unittest functions, then an import error will prevent any of your tests from running. I have two opinions about this. It depends how you're running your unittests.

If you're running tests on the commandline, or from Hudson or Jenkins, then you'll notice the import error and correct it immediately. In that case I don't think it's a problem to import everything at the module level. It's certainly more convenient, and requires less duplication.

If there's some chance that an import error will cause a silent failure (say, if your unittest framework can't even tell you that it failed unless it can import your test module), then it might be safer to import modules within your test functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消