def test_disabled_when_configured(self): spy = MasterSpy(EndlessFake()) self.context.inject(requests, spy) self.context.set_env(HTTPS_VERIFY_CERTS="FAlSe") HttpClient().get("http://spam") args, kwargs = spy.last_call_to("get") expect(kwargs).to(have_keys(verify=False))
def test_attribute_requested_returns_true_if_tracking_and_requested(self): stub = EmptyFake() stub.thing = None spy = MasterSpy(stub, affect_only_functions=False) spy.thing assert spy.attribute_was_requested( "thing"), "Spy did not report that attribute was requested"
def setUp(self): self.context = open_dependency_context(supply_env=True) self.http_proxy = "http://munchausen-gateway:4427" self.https_proxy = "http://special-something:7727" self.context.os.environ = {"HTTP_PROXY": self.http_proxy, "HTTPS_PROXY": self.https_proxy} self.session_spy = MasterSpy(FakeRequests()) self.requests_spy = MasterSpy(FakeRequests(self.session_spy)) self.context.inject(requests, self.requests_spy)
def test_enabled_in_session_by_default(self): requests_stub = EndlessFake() self.context.inject(requests, requests_stub) spy = MasterSpy(EndlessFake()) requests_stub.Session = lambda *a, **k: spy client = HttpClient() client.enable_cookies() client.get("http://spam") args, kwargs = spy.last_call_to("send") expect(kwargs).to(have_keys(verify=True))
def test_applies_affect_only_functions_flag_recursively(self): child = EmptyFake() child.value = 7 parent = EmptyFake() parent.get_child = lambda: child parent_spy = MasterSpy(parent, affect_only_functions=False) parent_spy.get_child() func_spy = parent_spy.attribute_spies["get_child"] ret_spy = func_spy.return_value_spies[0] ret_spy.value expect(ret_spy.attribute_spies.keys()).to(contain("value"))
def test_disabled_in_session_when_configured(self): self.context.set_env(HTTPS_VERIFY_CERTS="FAlSe") requests_stub = EndlessFake() self.context.inject(requests, requests_stub) spy = MasterSpy(EndlessFake()) requests_stub.Session = lambda *a, **k: spy client = HttpClient() client.enable_cookies() client.get("http://spam") args, kwargs = spy.last_call_to("send") expect(kwargs).to(have_keys(verify=False))
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.inject(webdriver, EndlessFake()) self.fake_tunnel_class = MasterSpy(EndlessFake()) self.context.inject(BrowserStackTunnel, self.fake_tunnel_class) self.context.set_env(browserstack_access_key="whatever", browserstack_url="gopher:hahah", browserstack_username="******")
def test_creates_local_binary_path(self): self.create_fake_archive() spy = MasterSpy(self.context.os.makedirs) self.context.os.makedirs = spy BrowserStackTunnel() assert spy.call_history, "makedirs was not called" args, kwargs = spy.call_history[-1] expect(args[0]).to(equal(self.local_binary_path))
def test_sets_full_screen(self): remote_spy = MasterSpy(EndlessFake()) self.webdriver_spy.Remote = lambda *a, **k: remote_spy Browser() expect(remote_spy.attribute_spies.keys()).to( contain("fullscreen_window")) calls = remote_spy.attribute_spies["fullscreen_window"].call_history assert calls, "fullscreen_window was not called"
def setUp(self): EventBroker.reset() self.context = open_dependency_context(supply_env=True, supply_fs=True) self.fake_file = FakeFile() self.context.inject(open, self.fake_file.open) self.makedirs_spy = MasterSpy(self.context.os.makedirs) self.context.os.makedirs = self.makedirs_spy self.sut = ArtifactSaver() self.sut.activate()
def test_closes_tunnel_on_suite_end(self): self.context.set_env(browser_locaton="BrowserStack", BROWSERSTACK_SET_LOCAL="true") spy = MasterSpy(TunnelToNowhere()) self.context.inject_as_class(BrowserStackTunnel, spy) sut = Browser() # noqa: F841 assert not spy.was_closed, "close was called prematurely." EventBroker.publish(event=TestEvent.suite_ended) assert spy.was_closed, "close was not called."
def test_saves_last_function_call(self): planted_args = (1, 4, 5) planted_kwargs = {"spam": 32, "eggs": object()} stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) spy.func(5, 7, 2, bruces=4, michaels=1) spy.func() spy.func(*planted_args, **planted_kwargs) expect(spy.attribute_spies["func"].call_history[-1]).to( equal((planted_args, planted_kwargs)))
def test_sets_executable_bits(self): self.create_fake_archive() spy = MasterSpy(self.context.os.chmod) self.context.os.chmod = spy BrowserStackTunnel() assert spy.call_history, "chmod was not called" args, kwargs = spy.call_history[-1] expect(args).to( equal((self.join_path(self.local_binary_path, self.local_binary_filename), 0o775)))
def setUp(self): EventBroker.reset() self.context = open_dependency_context(supply_env=True) defanged = copy(junit_xml.TestSuite) defanged.to_xml_string = lambda *args, **kwargs: "" self.spy = MasterSpy(defanged, affect_only_functions=False) self.context.inject(junit_xml.TestSuite, self.spy) self.results = SuiteResults() self.sut = JunitReporter() self.sut.activate()
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.set_env(BROWSERSTACK_ACCESS_KEY="fake") # Prevent real network activity self.context.inject_as_class(HttpClient, HttpStub()) self.context.set_env(BROWSER_ACCESS_KEY="spam") self.popen_spy = MasterSpy(PopenStub()) self.context.inject_as_class(Popen, self.popen_spy) # Thwart actual sleeping self.context.inject(time.sleep, lambda n: None) self.tunnel = BrowserStackTunnel()
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.set_env( BROWSER_AVAILABILITY_TIMEOUT=0, BROWSER_AVAILABILITY_THROTTLE=0, BROWSERSTACK_URL="https://browserstack.not:43/really", BROWSERSTACK_OS="CP/M", BROWSERSTACK_OS_VERSION="1.7", USE_BROWSER="NCSA-Mosaic", USE_BROWSER_VERSION="1.3", BROWSERSTACK_SCREEN_RESOLUTION="kumquat", BROWSER_AVALABILITY_TIMEOUT="0", BROWSERSTACK_SET_LOCAL="fAlSe", BROWSERSTACK_SET_DEBUG="TrUe", BROWSER_LOCATION="BrowserStack", BROWSERSTACK_USERNAME="******", BROWSERSTACK_ACCESS_KEY="gdf0i9/38md-p00", ) self.webdriver_spy = MasterSpy(EndlessFake()) self.context.inject(webdriver, self.webdriver_spy)
def test_last_call_to_returns_last_call_to_given_func(self): stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) planted_args = (4, 5) planted_kwargs = {"foo": 77, "bar": "soap"} spy.func(spam=1, eggs=2) spy.func(*planted_args, **planted_kwargs) expect(spy.last_call_to("func")).to( equal((planted_args, planted_kwargs)))
def test_saves_last_return_value(self): values = range(3) it = iter(range(3)) def func(): return next(it) spy = MasterSpy(func, affect_only_functions=False) for i in values: spy() expect(spy.return_value_spies[-1].unwrap_spy_target()).to( equal(values[-1]))
def test_last_call_to_returns_last_call_to_given_func(self): stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) planted_args = (4, 5) planted_kwargs = {'foo': 77, 'bar': 'soap'} spy.func(spam=1, eggs=2) spy.func(*planted_args, **planted_kwargs) expect(spy.last_call_to('func')).to( equal((planted_args, planted_kwargs)))
def setUp(self): EventBroker.reset() self.context = open_dependency_context(supply_env=True, supply_fs=True) self.fake_file = FakeFile() self.context.inject(open, self.fake_file.open) self.makedirs_spy = MasterSpy(self.context.os.makedirs) self.context.os.makedirs = self.makedirs_spy reports_path = "/where/oh/where" self.artifacts_path = os.path.join(reports_path, "artifacts") self.context.os.environ["reports_path"] = reports_path self.sut = ArtifactSaver() self.sut.activate()
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.set_env( BROWSERSTACK_ACCESS_KEY="lomein", BROWSERSTACK_URL="spam", BROWSERSTACK_USERNAME="******", BROWSER_LOCATION="BrowserStack", BROWSERSTACK_SET_LOCAL="True", ) self.fake_tunnel = EndlessFake() self.context.inject_as_class(BrowserStackTunnel, self.fake_tunnel) self.webdriver_spy = MasterSpy(EndlessFake()) self.context.inject(webdriver, self.webdriver_spy)
def test_saves_first_function_call(self): planted_args = (1, 4, 5) planted_kwargs = {'spam': 32, 'eggs': object()} stub = EmptyFake() stub.func = lambda *a, **k: None spy = MasterSpy(stub) spy.func(*planted_args, **planted_kwargs) spy.func(5, 7, 2, bruces=4, michaels=1) expect(spy.attribute_spies['func'].call_history[0]).to( equal((planted_args, planted_kwargs)))
class TestMemoryLeakProtection(TestCase): def setUp(self): self.context = open_dependency_context() Evidence.subscriber_ran = False self.fake_log = MasterSpy(EmptyFake()) self.context.inject(logging, self.fake_log) def tearDown(self): self.context.close() def test_bound_subscriber_gets_garbage_collected(self): # When the subscriber instantiates, it subscribes to an event # This causes the event broker to store a reference to the subscriber. # The test does not create a reference to the subscriber, so the broker # has the only reference. Subscriber() # Because the broker has the only reference, the subscriber should # be eligible for garbage collection. gc.collect() EventBroker.publish(event=EVENT) assert not Evidence.subscriber_ran, "Subscriber ran after it should have been garbage collected" def test_unbound_subscriber_gets_garbage_collected(self): def subscriber(): Evidence.subscriber_ran = True EventBroker.subscribe(event=EVENT, func=subscriber) subscriber = None gc.collect() EventBroker.publish(event=EVENT) assert not Evidence.subscriber_ran, "Subscriber ran after it should have been garbage collected" def errors_logged(self): if self.fake_log.attribute_was_requested("error"): spy = self.fake_log.attribute_spies["error"] return [args[0] for args, kwargs in spy.call_history] def test_does_not_attempt_to_run_garbage_collected_subscriber(self): def subscriber(): pass EventBroker.subscribe(event=EVENT, func=subscriber) subscriber = None gc.collect() EventBroker.publish(event=EVENT) assert not self.errors_logged( ), 'Unexpected errors in the log: "%s"' % (self.errors_logged())
def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.fake_http = FakeHttpClient() self.context.inject_as_class(HttpClient, self.fake_http) self.fake_popen = FakePopen() self.popen_spy = MasterSpy(self.fake_popen) self.context.inject(Popen, self.popen_spy) self.context.inject(logging, EndlessFake()) # Thwart actual sleeping self.context.inject(time.sleep, lambda n: None) self.binary_zip_url = "https://where-we-expect.the/binary.zip" local_binary = "/where/we/put/the/binary" self.local_binary_path, self.local_binary_filename = os.path.split( local_binary) self.context.set_env( BROWSERSTACK_ACCESS_KEY="whatever", BROWSERSTACK_LOCAL_BINARY_ZIP_URL=self.binary_zip_url, BROWSERSTACK_LOCAL_BINARY=local_binary, )
def test_call_returns_return_value(self): value = 27 spy = MasterSpy(lambda: value) expect(spy()).to(equal(value))
def setUp(self): self.context = open_dependency_context(supply_env=True) self.requests_spy = MasterSpy(FakeResponse()) self.context.inject(requests, self.requests_spy)
class TestRequestsPassthrough(TestCase): def setUp(self): self.context = open_dependency_context(supply_env=True) self.requests_spy = MasterSpy(FakeResponse()) self.context.inject(requests, self.requests_spy) def tearDown(self): self.context.close() def test_attribute_error_on_unknown_method(self): def attempt(): HttpClient().this_method_does_not_exist expect(attempt).to(raise_ex(AttributeError)) def check_request(self, method): url = "http://nothing" kwargs = {"spam": 42, "eggs": 0} getattr(HttpClient(), method)(url, **kwargs) req_args, req_kwargs = self.requests_spy.last_call_to(method) expect(req_args).to(equal((url, ))) for k, v in kwargs.items(): expect(req_kwargs).to(contain_key_with_value(k, v)) def check_response(self, method): actual = getattr(HttpClient(), method)("http://something") spy = self.requests_spy.attribute_spies[method] expect(actual).to(equal(spy.return_value_spies[-1])) def test_delete_request(self): self.check_request("delete") def test_delete_response(self): self.check_response("delete") def test_get_request(self): self.check_request("get") def test_get_response(self): self.check_response("get") def test_head_request(self): self.check_request("head") def test_head_response(self): self.check_response("head") def test_options_request(self): self.check_request("options") def test_options_response(self): self.check_response("options") def test_patch_request(self): self.check_response("patch") def test_post_request(self): self.check_request("post") def test_post_response(self): self.check_response("post") def test_put_request(self): self.check_request("put") def test_put_response(self): self.check_response("put")
def test_can_wrap_non_functions(self): target = EmptyFake() target.non_func = 42 spy = MasterSpy(target, affect_only_functions=False) expect(spy.non_func).to(be_a(MasterSpy))
class TestLaunchBrowserStack(TestCase): def setUp(self): self.context = open_dependency_context(supply_env=True, supply_fs=True) self.context.set_env( BROWSER_AVAILABILITY_TIMEOUT=0, BROWSER_AVAILABILITY_THROTTLE=0, BROWSERSTACK_URL="https://browserstack.not:43/really", BROWSERSTACK_OS="CP/M", BROWSERSTACK_OS_VERSION="1.7", USE_BROWSER="NCSA-Mosaic", USE_BROWSER_VERSION="1.3", BROWSERSTACK_SCREEN_RESOLUTION="kumquat", BROWSER_AVALABILITY_TIMEOUT="0", BROWSERSTACK_SET_LOCAL="fAlSe", BROWSERSTACK_SET_DEBUG="TrUe", BROWSER_LOCATION="BrowserStack", BROWSERSTACK_USERNAME="******", BROWSERSTACK_ACCESS_KEY="gdf0i9/38md-p00", ) self.webdriver_spy = MasterSpy(EndlessFake()) self.context.inject(webdriver, self.webdriver_spy) def tearDown(self): self.context.close() def test_complains_if_environment_lacks_username(self): self.context.unset_env("BROWSERSTACK_USERNAME") expect(Browser).to(raise_error(InvalidConfiguration)) def test_complains_if_environment_lacks_access_key(self): self.context.unset_env("BROWSERSTACK_ACCESS_KEY") expect(Browser).to(raise_error(InvalidConfiguration)) def parse_command_executor(self): args, kwargs = self.webdriver_spy.last_call_to("Remote") expect(kwargs.keys()).to(contain("command_executor")) return urlsplit(kwargs["command_executor"]) def retrieve_desired_capabilities(self): args, kwargs = self.webdriver_spy.last_call_to("Remote") expect(kwargs.keys()).to(contain("desired_capabilities")) return kwargs["desired_capabilities"] def test_uses_configured_browserstack_url(self): expected = "https://a.b.c/1" self.context.set_env(BROWSERSTACK_URL=expected) Browser() parsed = self.parse_command_executor() scheme, netloc, path, query, fragment = tuple(parsed) without_credentials = netloc.split("@")[-1] actual = urlunsplit( (scheme, without_credentials, path, query, fragment)) expect(actual).to(equal(expected)) def test_uses_urlencoded_configured_username(self): expected = "*****@*****.**" self.context.set_env(BROWSERSTACK_USERNAME=expected) Browser() parsed = self.parse_command_executor() expect(parsed.username).to(equal(quote_plus(expected))) def test_uses_urlencoded_configured_access_key(self): expected = "PleaseOhPlease" self.context.set_env(BROWSERSTACK_ACCESS_KEY=expected) Browser() parsed = self.parse_command_executor() expect(parsed.password).to(equal(expected)) def test_uses_configured_os(self): expected = "OS/irus" self.context.set_env(BROWSERSTACK_OS=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("os", expected)) def test_uses_configured_os_version(self): expected = "14.7.1" self.context.set_env(BROWSERSTACK_OS_VERSION=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("os_version", expected)) def test_uses_configured_screen_resolution(self): expected = "8x10" self.context.set_env(BROWSERSTACK_SCREEN_RESOLUTION=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("resolution", expected)) def test_uses_configured_browser_type(self): expected = "smoking" self.context.set_env(USE_BROWSER=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browser", expected)) def test_uses_configured_browser_version(self): expected = "nineteen" self.context.set_env(USE_BROWSER_VERSION=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browser_version", expected)) def test_does_not_specify_browser_version_if_not_configured(self): self.context.unset_env("USE_BROWSER_VERSION") Browser() expect(self.retrieve_desired_capabilities().keys()).not_to( contain("browser_version")) def test_uses_configured_browserstack_local(self): expected = False self.context.set_env(BROWSERSTACK_SET_LOCAL=expected) Browser() expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browserstack.local", expected)) def test_uses_configured_browserstack_debug(self): expected = True Browser() self.context.set_env(BROWSERSTACK_SET_DEBUG=expected) expect(self.retrieve_desired_capabilities()).to( contain_key_with_value("browserstack.debug", expected)) def test_sets_full_screen(self): remote_spy = MasterSpy(EndlessFake()) self.webdriver_spy.Remote = lambda *a, **k: remote_spy Browser() expect(remote_spy.attribute_spies.keys()).to( contain("fullscreen_window")) calls = remote_spy.attribute_spies["fullscreen_window"].call_history assert calls, "fullscreen_window was not called" def test_detects_busy_signal(self): def busy(*args, **kwargs): raise WebDriverException( "All parallel tests are currently in use, " "including the queued tests. Please wait to " "finish or upgrade your plan to add more sessions.") self.webdriver_spy.Remote = busy expect(Browser).to(raise_ex(AllBrowsersBusy))
def __init__(self): self.call_history = [] self.session_spy = MasterSpy(FakeResponse())