def test_meta_declaration(self): """Tests that false declarations of implementing an API raise an error""" assert Utils.not_raises(API.APIError, self.declare_basic_support) assert Utils.not_raises(API.APIError, self.declare_inherited_support) assert Utils.raises(API.APIError, self.declare_false_support) assert Utils.raises(API.APIError, self.declare_false_inherited_support) assert Utils.raises(API.APIError, self.declare_ill_support)
def test_identify(self): """Tests finding the names mapped to constants (and failure to find)""" assert MyStringEnum.identify("Big Apple") == "NEW_YORK" assert MyStringEnum.identify("A Rafting Opportunity") == "ORANGE_RIVER" assert Utils.raises(KeyError, MyStringEnum.identify, 1) assert Utils.raises(KeyError, MyStringEnum.identify, "new_york") assert MyStringEnum.identify("Quartzitic Sandstone", "TABLE_MOUNTAIN") == "TABLE_MOUNTAIN"
def test_skip_test_for(): """Skips the test when the right options are passed""" badgers = {"william": "holland", "eric": "new zealand", "cecily": "paraguay"} @Utils.skip_test_for("There is no badger", Utils.contains_expected_kwargs(badger="scott")) def find_badger(badger): """tries to find the given badger""" return badgers[badger] assert Utils.method_raises(Utils.Skipped)(find_badger)("scott") assert find_badger("william") == "holland"
def test_skip_test_for(): """Skips the test when the right options are passed""" badgers = { "william": "holland", "eric": "new zealand", "cecily": "paraguay" } @Utils.skip_test_for("There is no badger", Utils.contains_expected_kwargs(badger="scott")) def find_badger(badger): """tries to find the given badger""" return badgers[badger] assert Utils.method_raises(Utils.Skipped)(find_badger)("scott") assert find_badger("william") == "holland"
def test_lookup(self): """Tests looking up an enum by name""" assert MyStringEnum.lookup("NEW_YORK") == "Big Apple" assert MyStringEnum.lookup("ORANGE_RIVER") == "A Rafting Opportunity" assert Utils.raises(KeyError, MyStringEnum.lookup, "TABLE_MOUNTAIN") assert MyStringEnum.lookup( "TABLE_MOUNTAIN", "Quartzitic Sandstone") == "Quartzitic Sandstone"
def test_find_module(self): target_file = Module.__file__ if target_file.endswith(".pyc") or target_file.endswith(".pyo"): source_file = target_file[:target_file.rfind(".")] + ".py" if os.path.exists(source_file): target_file = source_file assert Module.find_module("j5basic") == os.path.dirname(target_file) assert Module.find_module("j5basic.Module") == target_file assert Utils.raises(ValueError, Module.find_module, "j5basic.NonExistent")
def test_secure_tmp_file(): file, file_name = Utils.secure_tmp_file(text=True) try: assert os.path.exists(file_name) file.write("Contents") file.close() assert os.path.exists(file_name) reader = open(file_name, "r") assert reader.read() == "Contents" reader.close() finally: os.remove(file_name) assert not os.path.exists(file_name)
def test_secure_tmp_file(): file, file_name = Utils.secure_tmp_file() try: assert os.path.exists(file_name) file.write("Contents".encode('utf-8')) file.close() assert os.path.exists(file_name) reader = open(file_name, "r") assert reader.read() == "Contents" reader.close() finally: os.remove(file_name) assert not os.path.exists(file_name)
def test_simple_raises_returns(): """check returning a value causes an error in raises""" raised = None returned = None try: returned = Utils.raises(ValueError, sample_method, 1) except AssertionError as e: raised = e except Exception as e: raised = e assert returned is None assert isinstance(raised, AssertionError) assert "Call to sample_method did not raise ValueError but returned 'pigeons'" == str(raised)
def test_simple_raises_returns(): """check returning a value causes an error in raises""" raised = None returned = None try: returned = Utils.raises(ValueError, sample_method, 1) except AssertionError as e: raised = e except Exception as e: raised = e assert returned is None assert isinstance(raised, AssertionError) assert "Call to sample_method did not raise ValueError but returned 'pigeons'" == str( raised)
def test_simple_raises_incorrect(): """checks that raises handles an incorrect exception properly""" raised = None returned = None try: returned = Utils.raises(ValueError, sample_method, 4) except AssertionError as e: raised = e except Exception as e: raised = e assert returned is None assert isinstance(raised, AssertionError) print(str(raised)) assert "Call to sample_method did not raise ValueError but raised KeyError: 'Wrong key'" == str(raised)
def test_min_max(self): assert Utils.raises(ValueError, min, self.empty) assert Utils.raises(ValueError, max, self.empty) assert self.single_item.min() == self.single_item.max() == 1 assert self.hundred.min() == 1 assert self.hundred.max() == 100 assert self.date.min() == ORWELL assert self.date.max() == SPACE assert self.space_year.min() == datetime.datetime(2001, 1, 1) assert self.space_year.max() == datetime.datetime(2001, 12, 31) # test altering operations hundred = self.hundred.copy() assert hundred.min() == 1 assert hundred.max() == 100 hundred.add(1000) assert hundred.max() == 1000 hundred.discard(1) assert hundred.min() == 2 hundred.remove(2) assert hundred.min() == 3 hundred |= self.single_item assert hundred.min() == 1 hundred &= self.hundred assert hundred.max() == 100
def test_resolve(self): assert Module.resolvemodule("j5basic.Module") == Module # Run this twice to cover the cached usage assert Module.resolvemodule("j5basic.Module") == Module try: from j5.Config import ConfigTree assert Module.resolvemodule( "j5.Config.ConfigTree.Node") == ConfigTree.Node except ImportError: pass assert Module.resolvemodule( "j5basic.Module.canonicalize") == Module.canonicalize assert Utils.raises(ImportError, Module.resolvemodule, "j5basic.Moodle")
def test_scalar_calculation(): """tests that scalars created from units can be used in calculations""" m = Units.BaseUnit("m") s = Units.BaseUnit("s") mm = m / 1000 x = m(35) assert x == mm(35000) assert m(35) + m(3) == m(38) assert m(35) - mm(3) == mm(34997) assert Utils.raises(ValueError, operator.add, m(35), 3) assert m(35) * 3 == m(105) assert m(35) * m(3) == (m * m)(35 * 3) assert Utils.raises(ValueError, operator.add, 3, m(35)) assert 3 * m(35) == m(105) assert m(35) * m(3) == (m * m)(35 * 3) assert s(12.0) / 4.0 == s(3.0) assert 12.0 / s(4.0) == (1 / s)(3.0) assert -s(5) == s(1) - s(6) assert +s(5) == s(1) + s(4) assert abs(s(-4)) == s(4) # These conversions are of debatable value, but are included at the moment assert float(m(35.0)) == 35.0 assert int(m(35.2)) == 35 assert int(mm(33000)) == 33000
def test_scalar_calculation(): """tests that scalars created from units can be used in calculations""" m = Units.BaseUnit("m") s = Units.BaseUnit("s") mm = m / 1000 x = m(35) assert x == mm(35000) assert m(35) + m(3) == m(38) assert m(35) - mm(3) == mm(34997) assert Utils.raises(ValueError, operator.add, m(35), 3) assert m(35) * 3 == m(105) assert m(35) * m(3) == (m*m)(35*3) assert Utils.raises(ValueError, operator.add, 3, m(35)) assert 3 * m(35) == m(105) assert m(35) * m(3) == (m*m)(35*3) assert s(12.0) / 4.0 == s(3.0) assert 12.0 / s(4.0) == (1/s)(3.0) assert -s(5) == s(1) - s(6) assert +s(5) == s(1) + s(4) assert abs(s(-4)) == s(4) # These conversions are of debatable value, but are included at the moment assert float(m(35.0)) == 35.0 assert int(m(35.2)) == 35 assert int(mm(33000)) == 33000
def test_simple_raises_incorrect(): """checks that raises handles an incorrect exception properly""" raised = None returned = None try: returned = Utils.raises(ValueError, sample_method, 4) except AssertionError as e: raised = e except Exception as e: raised = e assert returned is None assert isinstance(raised, AssertionError) print(str(raised)) assert "Call to sample_method did not raise ValueError but raised KeyError: 'Wrong key'" == str( raised)
return browser_name == "firefox" class SeleniumNotRunning(Utils.Skipped): pass class SeleniumNotInstalled(Utils.Skipped): pass def browser_is_notrunning(target, *args, **kwargs): """A checker that checks if the browser argument is a SeleniumNotRunning exception""" browser = Decorators.get_or_pop_arg("browser", args, kwargs, Decorators.inspect.getargspec(target)) if isinstance(browser, SeleniumNotRunning): return True return False if_selenium_browser = Utils.skip_test_for("Selenium Server not found; cannot run web browser tests", browser_is_notrunning) if_selenium_module = Utils.if_module(selenium, "selenium") if_selenium = Decorators.chain_decorators(if_selenium_browser, if_selenium_module) class BrowserDim(IterativeTester.Dimension): seleniumHost = "localhost" seleniumPort = 4444 def __init__(self): """iterates over supported browsers and runs tests on each of them""" browsernames = [] self._resources = {} self._browsernames = {} self._skipped_conditions = {} self._selenium_runners = {} havebrowsers = False
def test_expect_external_error_for(): """Checks that expected errors are skipped when the right options are passed""" wrapper = Utils.expect_external_error_for( KeyError, "Key errors for 4", Utils.contains_expected_kwargs(option=4))(sample_method) # check that when passing through the expected option the correct error is raised assert Utils.method_raises(Utils.ExpectedExternalError)(wrapper)(4) assert Utils.method_raises(Utils.ExpectedExternalError)(wrapper)(option=4) # check that when passing in different options an exception comes through normally assert Utils.method_raises(ValueError)(wrapper)(option=3) # check that when passing in different options a return value comes through normally assert wrapper(1) == "pigeons" assert wrapper(option=1) == "pigeons" # check that an assertion is raised if the expected error is not raised wrapper = Utils.expect_external_error_for( KeyError, "Key errors for 4", Utils.contains_expected_kwargs(option=3))(sample_method) assert Utils.method_raises(AssertionError)(wrapper)(option=3) wrapper = Utils.expect_external_error_for( KeyError, "Key errors for 4", Utils.contains_expected_kwargs(option=1))(sample_method) assert Utils.method_raises(AssertionError)(wrapper)(option=1)
def test_catchskip(): """Tests that a properly decorated method is skipped - this must be run...""" assert True Utils.skip( "This method should be skipped, as it is testing skipping methods") raise AssertionError("This test should have been skipped")
def test_skiptest(): """checks that this method will be skipped""" assert True Utils.skip( "This method should be skipped, as it is testing skipping methods") raise AssertionError("This test should have been skipped")
def test_lookup(self): """Tests looking up an enum by name""" assert MyEnum.lookup("APPLES") == 1 assert MyEnum.lookup("ORANGES") == 2 assert Utils.raises(KeyError, MyEnum.lookup, "PEARS") assert MyEnum.lookup("PEARS", 4) == 4
def test_simple_raises_correct(): """check raising the correct error, and a subclass of the correct error""" assert Utils.raises(ValueError, sample_method, 2) assert Utils.raises(ValueError, sample_method, 3)
def test_identify(self): """Tests finding the names mapped to constants (and failure to find)""" assert MyEnum.identify(1) == "APPLES" assert MyEnum.identify(2) == "ORANGES" assert Utils.raises(KeyError, MyEnum.identify, 3) assert MyEnum.identify(3, "MONKEY") == "MONKEY"
def skipmeth(self): Utils.skip(message)
def test_expect_external_error_for(): """Checks that expected errors are skipped when the right options are passed""" wrapper = Utils.expect_external_error_for(KeyError, "Key errors for 4", Utils.contains_expected_kwargs(option=4))(sample_method) # check that when passing through the expected option the correct error is raised assert Utils.method_raises(Utils.ExpectedExternalError)(wrapper)(4) assert Utils.method_raises(Utils.ExpectedExternalError)(wrapper)(option=4) # check that when passing in different options an exception comes through normally assert Utils.method_raises(ValueError)(wrapper)(option=3) # check that when passing in different options a return value comes through normally assert wrapper(1) == "pigeons" assert wrapper(option=1) == "pigeons" # check that an assertion is raised if the expected error is not raised wrapper = Utils.expect_external_error_for(KeyError, "Key errors for 4", Utils.contains_expected_kwargs(option=3))(sample_method) assert Utils.method_raises(AssertionError)(wrapper)(option=3) wrapper = Utils.expect_external_error_for(KeyError, "Key errors for 4", Utils.contains_expected_kwargs(option=1))(sample_method) assert Utils.method_raises(AssertionError)(wrapper)(option=1)
def test_catchskip(): """Tests that a properly decorated method is skipped - this must be run...""" assert True Utils.skip("This method should be skipped, as it is testing skipping methods") raise AssertionError("This test should have been skipped")
def test_skipped_printable(): """Tests that skipped objects are printable""" error = Utils.Skipped("The Message") assert "The Message" in str(error)
def test_lookup(self): """Tests looking up an enum by name""" assert MyStringEnum.lookup("NEW_YORK") == "Big Apple" assert MyStringEnum.lookup("ORANGE_RIVER") == "A Rafting Opportunity" assert Utils.raises(KeyError, MyStringEnum.lookup, "TABLE_MOUNTAIN") assert MyStringEnum.lookup("TABLE_MOUNTAIN", "Quartzitic Sandstone") == "Quartzitic Sandstone"
def test_simple_not_raises_returns(): """check returning a value doesn't cause an error in not_raises""" assert Utils.not_raises(ValueError, sample_method, 1)
def test_skiptest(): """checks that this method will be skipped""" assert True Utils.skip("This method should be skipped, as it is testing skipping methods") raise AssertionError("This test should have been skipped")