Stop unittest from running code from import module
up vote
1
down vote
favorite
Got 2 files :
new_project
├── Main.py
└── testing.py
I run:
$ cd new_project
$ python -m unittest testing.py
My whole testing file is :
import unittest
from Main import Square
class TestSquare(unittest.TestCase):
def test_init(self):
self.assertEqual(Square(0,0).center, Point(50,50))
print("test")
With Square being the first class in my Main.py file.
Main.py is composed like below :
import sys
import math
import random
def d_print(message):
print(message, file=sys.stderr)
class Square:
# on découpe le terrain en square et on les marque avec top right corner of square
def __init__(self, x, y):
self.xtr = x
self.ytr = y
self.size = 100
self.explored = False
self.center = Point(x+50, y+50)
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The code inside the while loop will be called every turn by a game simulator. The simulator provides inputs.
When I run the unittest command line, it actually waits for inputs
If I remove the import Main and the TestFixture on Square, the unittest passes. I tried several configurations and I can't manage to import Square for the test without having the while loop being start.
So when I import only a class from Main.py, it still runs the code outside the class. It's boggling my mind. Here is the link to import mechanism and I don't get why it runs the code and how to prevent it https://docs.python.org/3/reference/import.html
Since the game simulation is not in my control, I can't change much about the way Main.py is written. My only idea is to split the loop and the classes in two files for dev and testing ; when submitting I would have to concatenate the files back into one.
So (thanks for reading so far ;) :
- why are unittest/import working that way ?
- any idea on how to solve it ? (I am trying the split files idea right now, will report back)
python-3.x python-import python-unittest
add a comment |
up vote
1
down vote
favorite
Got 2 files :
new_project
├── Main.py
└── testing.py
I run:
$ cd new_project
$ python -m unittest testing.py
My whole testing file is :
import unittest
from Main import Square
class TestSquare(unittest.TestCase):
def test_init(self):
self.assertEqual(Square(0,0).center, Point(50,50))
print("test")
With Square being the first class in my Main.py file.
Main.py is composed like below :
import sys
import math
import random
def d_print(message):
print(message, file=sys.stderr)
class Square:
# on découpe le terrain en square et on les marque avec top right corner of square
def __init__(self, x, y):
self.xtr = x
self.ytr = y
self.size = 100
self.explored = False
self.center = Point(x+50, y+50)
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The code inside the while loop will be called every turn by a game simulator. The simulator provides inputs.
When I run the unittest command line, it actually waits for inputs
If I remove the import Main and the TestFixture on Square, the unittest passes. I tried several configurations and I can't manage to import Square for the test without having the while loop being start.
So when I import only a class from Main.py, it still runs the code outside the class. It's boggling my mind. Here is the link to import mechanism and I don't get why it runs the code and how to prevent it https://docs.python.org/3/reference/import.html
Since the game simulation is not in my control, I can't change much about the way Main.py is written. My only idea is to split the loop and the classes in two files for dev and testing ; when submitting I would have to concatenate the files back into one.
So (thanks for reading so far ;) :
- why are unittest/import working that way ?
- any idea on how to solve it ? (I am trying the split files idea right now, will report back)
python-3.x python-import python-unittest
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Got 2 files :
new_project
├── Main.py
└── testing.py
I run:
$ cd new_project
$ python -m unittest testing.py
My whole testing file is :
import unittest
from Main import Square
class TestSquare(unittest.TestCase):
def test_init(self):
self.assertEqual(Square(0,0).center, Point(50,50))
print("test")
With Square being the first class in my Main.py file.
Main.py is composed like below :
import sys
import math
import random
def d_print(message):
print(message, file=sys.stderr)
class Square:
# on découpe le terrain en square et on les marque avec top right corner of square
def __init__(self, x, y):
self.xtr = x
self.ytr = y
self.size = 100
self.explored = False
self.center = Point(x+50, y+50)
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The code inside the while loop will be called every turn by a game simulator. The simulator provides inputs.
When I run the unittest command line, it actually waits for inputs
If I remove the import Main and the TestFixture on Square, the unittest passes. I tried several configurations and I can't manage to import Square for the test without having the while loop being start.
So when I import only a class from Main.py, it still runs the code outside the class. It's boggling my mind. Here is the link to import mechanism and I don't get why it runs the code and how to prevent it https://docs.python.org/3/reference/import.html
Since the game simulation is not in my control, I can't change much about the way Main.py is written. My only idea is to split the loop and the classes in two files for dev and testing ; when submitting I would have to concatenate the files back into one.
So (thanks for reading so far ;) :
- why are unittest/import working that way ?
- any idea on how to solve it ? (I am trying the split files idea right now, will report back)
python-3.x python-import python-unittest
Got 2 files :
new_project
├── Main.py
└── testing.py
I run:
$ cd new_project
$ python -m unittest testing.py
My whole testing file is :
import unittest
from Main import Square
class TestSquare(unittest.TestCase):
def test_init(self):
self.assertEqual(Square(0,0).center, Point(50,50))
print("test")
With Square being the first class in my Main.py file.
Main.py is composed like below :
import sys
import math
import random
def d_print(message):
print(message, file=sys.stderr)
class Square:
# on découpe le terrain en square et on les marque avec top right corner of square
def __init__(self, x, y):
self.xtr = x
self.ytr = y
self.size = 100
self.explored = False
self.center = Point(x+50, y+50)
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The code inside the while loop will be called every turn by a game simulator. The simulator provides inputs.
When I run the unittest command line, it actually waits for inputs
If I remove the import Main and the TestFixture on Square, the unittest passes. I tried several configurations and I can't manage to import Square for the test without having the while loop being start.
So when I import only a class from Main.py, it still runs the code outside the class. It's boggling my mind. Here is the link to import mechanism and I don't get why it runs the code and how to prevent it https://docs.python.org/3/reference/import.html
Since the game simulation is not in my control, I can't change much about the way Main.py is written. My only idea is to split the loop and the classes in two files for dev and testing ; when submitting I would have to concatenate the files back into one.
So (thanks for reading so far ;) :
- why are unittest/import working that way ?
- any idea on how to solve it ? (I am trying the split files idea right now, will report back)
python-3.x python-import python-unittest
python-3.x python-import python-unittest
asked Nov 10 at 11:10
Poutrathor
96421332
96421332
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
To avoid running the loop, when the file is imported as a module, write it this way:
if __name__ == "__main__":
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The answer is simple, when you import the module the code on that file will be executed, which will cause the loop to run and the execution to be blocked on the input()
. For more details about how it works please read this answer with a detailed explanation
Why does import execute the code ? Isn't it dangerous ? if someone add code to a module for example ?
– Poutrathor
Nov 10 at 18:54
it does work ! thanks for the tip :) I tried to usif != main: sys.exit()
to avoid having a lot of code at an indentation of 1 but that failed since it's executed before the tests. Any syntax I could use to avoid the dread indent many code lines ?
– Poutrathor
Nov 10 at 19:35
This condition on the module level is the standard way of doing the separation between the cases when a file is executed vs when it is imported in python. I did not understand what you tried to achieve with that line.
– dethos
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
To avoid running the loop, when the file is imported as a module, write it this way:
if __name__ == "__main__":
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The answer is simple, when you import the module the code on that file will be executed, which will cause the loop to run and the execution to be blocked on the input()
. For more details about how it works please read this answer with a detailed explanation
Why does import execute the code ? Isn't it dangerous ? if someone add code to a module for example ?
– Poutrathor
Nov 10 at 18:54
it does work ! thanks for the tip :) I tried to usif != main: sys.exit()
to avoid having a lot of code at an indentation of 1 but that failed since it's executed before the tests. Any syntax I could use to avoid the dread indent many code lines ?
– Poutrathor
Nov 10 at 19:35
This condition on the module level is the standard way of doing the separation between the cases when a file is executed vs when it is imported in python. I did not understand what you tried to achieve with that line.
– dethos
2 days ago
add a comment |
up vote
2
down vote
To avoid running the loop, when the file is imported as a module, write it this way:
if __name__ == "__main__":
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The answer is simple, when you import the module the code on that file will be executed, which will cause the loop to run and the execution to be blocked on the input()
. For more details about how it works please read this answer with a detailed explanation
Why does import execute the code ? Isn't it dangerous ? if someone add code to a module for example ?
– Poutrathor
Nov 10 at 18:54
it does work ! thanks for the tip :) I tried to usif != main: sys.exit()
to avoid having a lot of code at an indentation of 1 but that failed since it's executed before the tests. Any syntax I could use to avoid the dread indent many code lines ?
– Poutrathor
Nov 10 at 19:35
This condition on the module level is the standard way of doing the separation between the cases when a file is executed vs when it is imported in python. I did not understand what you tried to achieve with that line.
– dethos
2 days ago
add a comment |
up vote
2
down vote
up vote
2
down vote
To avoid running the loop, when the file is imported as a module, write it this way:
if __name__ == "__main__":
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The answer is simple, when you import the module the code on that file will be executed, which will cause the loop to run and the execution to be blocked on the input()
. For more details about how it works please read this answer with a detailed explanation
To avoid running the loop, when the file is imported as a module, write it this way:
if __name__ == "__main__":
while True:
# do stuff every turn
x, y, value = [int(j) for j in input().split()]
The answer is simple, when you import the module the code on that file will be executed, which will cause the loop to run and the execution to be blocked on the input()
. For more details about how it works please read this answer with a detailed explanation
edited Nov 10 at 15:52
answered Nov 10 at 15:47
dethos
1,012811
1,012811
Why does import execute the code ? Isn't it dangerous ? if someone add code to a module for example ?
– Poutrathor
Nov 10 at 18:54
it does work ! thanks for the tip :) I tried to usif != main: sys.exit()
to avoid having a lot of code at an indentation of 1 but that failed since it's executed before the tests. Any syntax I could use to avoid the dread indent many code lines ?
– Poutrathor
Nov 10 at 19:35
This condition on the module level is the standard way of doing the separation between the cases when a file is executed vs when it is imported in python. I did not understand what you tried to achieve with that line.
– dethos
2 days ago
add a comment |
Why does import execute the code ? Isn't it dangerous ? if someone add code to a module for example ?
– Poutrathor
Nov 10 at 18:54
it does work ! thanks for the tip :) I tried to usif != main: sys.exit()
to avoid having a lot of code at an indentation of 1 but that failed since it's executed before the tests. Any syntax I could use to avoid the dread indent many code lines ?
– Poutrathor
Nov 10 at 19:35
This condition on the module level is the standard way of doing the separation between the cases when a file is executed vs when it is imported in python. I did not understand what you tried to achieve with that line.
– dethos
2 days ago
Why does import execute the code ? Isn't it dangerous ? if someone add code to a module for example ?
– Poutrathor
Nov 10 at 18:54
Why does import execute the code ? Isn't it dangerous ? if someone add code to a module for example ?
– Poutrathor
Nov 10 at 18:54
it does work ! thanks for the tip :) I tried to us
if != main: sys.exit()
to avoid having a lot of code at an indentation of 1 but that failed since it's executed before the tests. Any syntax I could use to avoid the dread indent many code lines ?– Poutrathor
Nov 10 at 19:35
it does work ! thanks for the tip :) I tried to us
if != main: sys.exit()
to avoid having a lot of code at an indentation of 1 but that failed since it's executed before the tests. Any syntax I could use to avoid the dread indent many code lines ?– Poutrathor
Nov 10 at 19:35
This condition on the module level is the standard way of doing the separation between the cases when a file is executed vs when it is imported in python. I did not understand what you tried to achieve with that line.
– dethos
2 days ago
This condition on the module level is the standard way of doing the separation between the cases when a file is executed vs when it is imported in python. I did not understand what you tried to achieve with that line.
– dethos
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238353%2fstop-unittest-from-running-code-from-import-module%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password