1

I have a project which as the actual structure:

Project
   |-setup.py
   |
   |-Code
   |  |-exemple.py
   |
   |-Test
      |-testExemple.py

I want to make my setup automated to set the files and path directories whatever they are. So in my Setup.py, I have like 30 imports,but then I have inputs:

testFolderNameSetup = input("Enter the name of the folder containing your tests:")
testFolderNameSetup = "./" + str(testFolderNameSetup)
testFolderPathNameSetup = (os.path.abspath(os.path.join(os.path.dirname(__file__), testFolderNameSetup)))

if os.path.exists(testFolderPathNameSetup):
     sys.path.insert(0, testFolderPathNameSetup)

the thing is, when I import setup in my testExample.py like this

from setup import *

and I lauch unit tests, I receive this error message

Testing started at 9:02 AM ...
C:\Users\marc-\AppData\Local\Continuum\anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pycharm\_jb_pytest_runner.py" --target testExemple.py::TestExample.testAdd
Launching py.test with arguments testExemple.py::TestExample::testAdd in C:\Users\marc-\Documents\GitHub\BasicProjectArchitecture\test

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: C:\Users\marc-\Documents\GitHub\BasicProjectArchitecture, inifile:Enter the name of the folder containing your code:
test/testExemple.py:None (test/testExemple.py)
testExemple.py:2: in <module>
    from setup import *
..\setup.py:21: in <module>
    codeFolderNameSetup = get_input("Enter the name of the folder containing your code:")
..\setup.py:14: in get_input
    return input(text)
..\..\..\..\AppData\Local\Continuum\anaconda3\lib\site-packages\_pytest\capture.py:459: in read
    raise IOError("reading from stdin while output is captured")
E   OSError: reading from stdin while output is captured

=================================== ERRORS ====================================
____________________ ERROR collecting test/testExemple.py _____________________
testExemple.py:2: in <module>
    from setup import *
..\setup.py:21: in <module>
    codeFolderNameSetup = get_input("Enter the name of the folder containing your code:")
..\setup.py:14: in get_input
    return input(text)
..\..\..\..\AppData\Local\Continuum\anaconda3\lib\site-packages\_pytest\capture.py:459: in read
    raise IOError("reading from stdin while output is captured")
E   OSError: reading from stdin while output is captured
------------------------------- Captured stdout -------------------------------
Enter the name of the folder containing your code:
=========================== 1 error in 0.22 seconds ===========================
ERROR: not found: C:\Users\marc-\Documents\GitHub\BasicProjectArchitecture\test\testExemple.py::TestExample::testAdd
(no name 'C:\\Users\\marc-\\Documents\\GitHub\\BasicProjectArchitecture\\test\\testExemple.py::TestExample::testAdd' in any of [<Module 'test/testExemple.py'>])

Process finished with exit code 0

Yes the error is get_input, just because I tried replaceing input by a function returning the input called get_input, but it still doesn't work. I have heard about mocking inputs, but I don't think I should do that, I'm not trying to test inputs, I just want to import a file that uses inputs in its script...

If anyone has an idea how to resolve this, please help!

Thank you!

--------------------EDIT #1-----------------

It seems that when I import setup.py, it launches it! I tried import in a basic function and it runs the setup code... That might be why the unitest doesn,t work. I'll try to work it out and come back with an answer.

Community
  • 1
  • 1
  • Does https://stackoverflow.com/questions/38723140/i-want-to-use-stdin-in-a-pytest-test help at all? – Tom Dalton May 15 '18 at 14:31
  • What do you expect/want to happen at the point at which `codeFolderNameSetup = get_input("Enter the name of the folder containing your code:")` is reached in `setup.py`? – Tom Dalton May 15 '18 at 14:32
  • I want simply to create a path to that folder, but the issue is just that I want all my imports and my setup to be made in setup.py, but When I import it, it launches setup.py, which is why the exempleTest fails. I want to be able to make a interactive setup file that I can then import in all my files and be good, but also be able to lauch it to configure fisrt project settup. –  May 15 '18 at 14:36
  • No, this isn't a mock problem, I though it was one, but i'm not using any input in the actual testing. –  May 15 '18 at 14:37
  • Your tests import `setup.py`, which uses `input`. Maybe your setup.py sile shouldn't be using `input`? Without you posting the contents of all your file,s it's not 100% clear to me what exactly you are doing and what you are intending/wanting to do. – Tom Dalton May 15 '18 at 14:58
  • Thanks for trying, I resolved it, you can look at the answer. I'll post the github if you're curious, it is in the branch setup upgrade. https://github.com/PyMarc2/BasicProjectArchitecture –  May 15 '18 at 15:16

1 Answers1

0

Problem resolved.

Simply and beautifully, there's a line in python that tells to the code to run only if it is launched directly.

if __name__ == "__main__"

I believe that the file as an inherent __name__ that is given when launched and this name is "__main__" only when launched directly. Thus, I can import setup whithout having the trouble of having it launch in my face and messing with my codes and tests.

Good day!

Tom Dalton
  • 6,122
  • 24
  • 35