def they_have_default_failure_message(self): def predicate_with_bad_name(thing): return False add_expectation(predicate_with_bad_name) assert fail_msg(expect('walrus').predicate_with_bad_name) == ( "Expected that 'walrus' predicate_with_bad_name, but got False")
def they_can_fail(self): add_expectation(self.is_a_potato) def _fails(): expect('not a potato').is_a_potato() assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( "Expected that 'not a potato' is_a_potato, but it isn't")
def they_have_default_failure_message(self): def predicate_with_bad_name(thing): return False add_expectation(predicate_with_bad_name) assert fail_msg(expect("walrus").predicate_with_bad_name) == ( "Expected that 'walrus' predicate_with_bad_name, but got False" )
def they_can_fail(): add_expectation(is_a_potato) def _fails(): expect('not a potato').is_a_potato() with pytest.raises(AssertionError): _fails() assert fail_msg(_fails) == ( "Expected that 'not a potato' is a potato, but it isn't")
def they_adjust_failure_message_for_expectation_name(self): def can_do_something(thing): return False def will_do_something(thing): return False for predicate in [can_do_something, will_do_something]: add_expectation(predicate) assert fail_msg(expect('walrus').can_do_something) == ( "Expected that 'walrus' can_do_something, but it can't") assert fail_msg(expect('walrus').will_do_something) == ( "Expected that 'walrus' will_do_something, but it won't")
def they_can_succeed(self): add_expectation(self.is_a_potato) expect("potato").is_a_potato()
def they_can_succeed(self): add_expectation(self.is_a_potato) expect('potato').is_a_potato()
import expecter def has_text(browser, text): return browser.is_text_present(text) expecter.add_expectation(has_text)
def they_can_have_postional_arguments(): def is_a(thing, vegetable): return thing == vegetable add_expectation(is_a) expect('potato').is_a('potato')
import logging import expecter import log def contains_html(response, text): __tracebackhide__ = True # pylint: disable=unused-variable html = response.content.decode('utf-8') # TODO: make this a regular expression to check word boundaries if text in html: return True assert 0, (f"Expected:\n{html}\nto contain {text!r}, but it didn't") expecter.add_expectation(contains_html)
import json import logging import expecter import log def contains_json(response, **kwargs): __tracebackhide__ = True # pylint: disable=unused-variable data = response.json() missing = None for key, value in kwargs.items(): if data.get(key) == value: continue elif not missing: missing = {key: value} if not missing: return True data_s = json.dumps(data, indent=4, sort_keys=True) missing_s = json.dumps(missing, indent=None, sort_keys=True) assert 0, (f"Expected:\n{data_s}\nto contain {missing_s}, but it didn't") expecter.add_expectation(contains_json)
raise Exception('%s has no URL defined' % self.__class__.__name__) return self.url def get(self, *args, **kwargs): return self.client.get(self.get_url(), *args, **kwargs) def post(self, *args, **kwargs): return self.client.post(self.get_url(), *args, **kwargs) def login(self, user=None): if not user: user = factories.UserFactory.create() name = user.username # in tests all users have password set to their username self.client.login(username=name, password=name) return user def assert_requires_login(self): resp = self.get(follow=False) expecter.expect(resp).is_redirect() def is_success(response): return response.status_code == 200 expecter.add_expectation(is_success) def is_redirect(response): return response.status_code == 302 expecter.add_expectation(is_redirect)
def they_can_have_keyword_arguments(self): def is_a(thing, vegetable): return thing == vegetable add_expectation(is_a) expect("potato").is_a(vegetable="potato")
def they_can_have_keyword_arguments(): def is_a(thing, vegetable): return thing == vegetable add_expectation(is_a) expect('potato').is_a(vegetable='potato')
return x is True def is_false(x): return x is False def exists(x): return os.path.exists(x) def missing(x): return not os.path.exists(x) expecter.add_expectation(is_none) expecter.add_expectation(is_true) expecter.add_expectation(is_false) expecter.add_expectation(exists) expecter.add_expectation(missing) def strip(text, tabs=None, end='\n'): """Strip leading whitespace indentation on multiline string literals.""" lines = [] for line in text.strip().splitlines(): if not tabs: tabs = line.count(' ' * 4) lines.append(line.replace(' ' * tabs * 4, '', 1))
def expect_list_of_class(self, l, cls): for i in l: expect(i).isinstance(cls) def is_not_None(var): return var is not None def is_None(var): return var is None def is_True(var): return var is True def is_False(var): return var is False add_expectation(is_not_None) add_expectation(is_None) add_expectation(is_True) add_expectation(is_False) if sys.version_info >= (3, 0): str_test = (str, bytes) else: str_test = (str, unicode)