Last active
November 20, 2015 09:56
-
-
Save didrocks/ae12d61138090b837c18 to your computer and use it in GitHub Desktop.
subprocess and environment test behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" base init class, changing the bash environment for process """ | |
import os | |
from unittest import TestCase | |
class BaseTests(TestCase): | |
"""Large framework base utilities""" | |
def setUp(self): | |
print("setUp in BaseTests") | |
super().setUp() | |
self.original_env = os.environ.copy() | |
# change an exesting environment variable for all tests | |
os.environ["SHELL"] = "/bin/foo" | |
print("--- environ in process ---") | |
print(os.environ) | |
def tearDown(self): | |
os.environ = self.original_env | |
print("--- restored environ ---") | |
print(os.environ) | |
super().tearDown() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "Failing by running all tests" | |
export http_proxy="FOOPROXY" | |
nosetests3 -s . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
echo "Success by running only B tests" | |
export http_proxy="FOOPROXY" | |
nosetests3 -s test_b.py | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" First test class doing nothing """ | |
from . import BaseTests | |
class A(BaseTests): | |
def test_foo(self): | |
pass | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Real test class impacted by the presence of A """ | |
from . import BaseTests | |
import os | |
import subprocess | |
class B(BaseTests): | |
@classmethod | |
def setUpClass(cls): | |
super().setUpClass() | |
print("\n------------------------------\nRemoving http_proxy env variable for all tests of this class") | |
cls.proxy_env = os.environ.pop("http_proxy", None) | |
@classmethod | |
def tearDownClass(cls): | |
super().tearDownClass() | |
if cls.proxy_env: | |
os.environ["http_proxy"] = cls.proxy_env | |
def test_foo(self): | |
env_output = subprocess.check_output(["env"]).decode("utf-8") | |
print("--- env subprocess ---") | |
print(env_output) | |
self.assertNotIn("http_proxy=", env_output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment