Create a gist now

Instantly share code, notes, and snippets.

@didrocks /.gitignore
Last active Nov 20, 2015

What would you like to do?
subprocess and environment test behavior
__pycache__
""" 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()
#!/bin/bash
echo "Failing by running all tests"
export http_proxy="FOOPROXY"
nosetests3 -s .
#!/bin/sh
echo "Success by running only B tests"
export http_proxy="FOOPROXY"
nosetests3 -s test_b.py
""" First test class doing nothing """
from . import BaseTests
class A(BaseTests):
def test_foo(self):
pass
""" 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