3

"PyCharm knew that in testing you do the import inside the unit test not at module start" is a quote from the "Getting Started with PyCharm 7/8: Testing" video about testing capabilities provided by PyCharm.

From PEP8:

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

In this other SO question, the concept of where to import in python has already been addressed as a broder concept. However, there's no mention of the special case when unit testing.

What advantages would we get from importing inside tests instead of at the start of the module?

If different tests use the same modules, does the module have to be imported each time?

Alechan
  • 817
  • 1
  • 10
  • 24
  • 1
    Possible duplicate of [Should import statements always be at the top of a module?](https://stackoverflow.com/questions/128478/should-import-statements-always-be-at-the-top-of-a-module) – Qback Apr 25 '18 at 09:35
  • 3
    @Qback, none of the answers to that question talk about unit testing. I think this question is relevant when focusing on this use case. – Alechan Apr 25 '18 at 09:36
  • @Alechan There's no mention of the special case when unit testing because it is not a spetial case. Why do you think it is special? – Stop harming Monica Apr 25 '18 at 10:27
  • 2
    @Goyo because it's not referred to in the answers to that question and because testing something is different than defining the functionality of something. – Alechan Apr 25 '18 at 18:09
  • @Alechan Writing some piece of code is always different than writing some other piece of code. How is unit testing special with regard to imports? – Stop harming Monica Apr 25 '18 at 18:42

1 Answers1

0

According to this question it would be better to place your import at the beginning of your module.

If you place import at beginning of function the import will be available only inside this function, you won't be able to use it outside this function. Consider this example:

def func():
    import time
    time.sleep(1)

func()
time.sleep(1) # NameError: name 'time' is not defined

Also If you'll import your module inside function it'll be imported every time function is called as mentioned it this question too.

I think you should place import inside test only if you actually want to test this importing in concrete case.

Qback
  • 4,310
  • 3
  • 25
  • 38
  • 1
    Yes, but that question makes no mention of unit testing. Also, your last link should be to the answer that mentions the thing that you are referring, not to the question. – Alechan Apr 25 '18 at 09:50
  • @Alechan just edited to show my point. ;) But I think that *import* testing should be really rare case. If your code can't import something then it's probably installed wrong. – Qback Apr 25 '18 at 09:53
  • 1
    I think the reason is so only the tests that have failed imports fail, otherwise the whole module fails and no test is run. I'm not sure, that's why I asked the question here – Alechan Apr 25 '18 at 09:58
  • 1
    @Alechan Yes, that's what I pointed. If your intention is to test importing in concrete case, then place `import` inside *test case*. But in most cases you can place it at beginning of your *module* because if whole module will fail **doing import** then you probably have installed your code wrong and it shouldn't run at all. In most cases tests are intended to check if you wrote your code properly, not if you installed every dependency. – Qback Apr 25 '18 at 10:01
  • 1
    @Alechan But if e.g. fire your tests at first launch to check if your app can **import** everything properly, then feel free to test `import` ;) – Qback Apr 25 '18 at 10:05