How to use the pytest.raises function in pytest

To help you get started, we’ve selected a few pytest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ThomasBrouwer / BNMTF / tests / code / test_bnmf_gibbs_optimised.py View on Github external
BNMF = bnmf_gibbs_optimised(R,M,K,priors)
    BNMF.all_U = [numpy.ones((I,K)) for i in range(0,iterations)]
    BNMF.all_V = [2*numpy.ones((J,K)) for i in range(0,iterations)]
    BNMF.all_tau = [3. for i in range(0,iterations)]
    # expU*expV.T = [[6.]]
    
    log_likelihood = 3./2.*(math.log(3.)-math.log(2*math.pi)) - 3./2. * (5**2 + 4**2 + 2**2)
    AIC = -2*log_likelihood + 2*(2*3+2*3)
    BIC = -2*log_likelihood + (2*3+2*3)*math.log(3)
    MSE = (5**2+4**2+2**2)/3.
    
    assert log_likelihood == BNMF.quality('loglikelihood',burnin,thinning)
    assert AIC == BNMF.quality('AIC',burnin,thinning)
    assert BIC == BNMF.quality('BIC',burnin,thinning)
    assert MSE == BNMF.quality('MSE',burnin,thinning)
    with pytest.raises(AssertionError) as error:
        BNMF.quality('FAIL',burnin,thinning)
    assert str(error.value) == "Unrecognised metric for model quality: FAIL."
github ethereum / py-trie / tests / test_hexary_trie.py View on Github external
assert encode_hex(trie_root_hash) in set_exc_message
    assert encode_hex(first_child_hash) in set_exc_message

    # Get exception when trying to delete key with missing data
    with pytest.raises(MissingTrieNode) as delete_exc_info:
        trie.delete(key1)

    delete_exc_message = str(delete_exc_info.value)

    assert encode_hex(key1) in delete_exc_message
    assert encode_hex(trie_root_hash) in delete_exc_message
    assert encode_hex(first_child_hash) in delete_exc_message

    # Get exception when checking if key exists with missing data
    key1_shared_prefix2 = to_bytes(0x0345)
    with pytest.raises(MissingTrieNode) as existance_exc_info:
        key1_shared_prefix2 in trie

    existance_exc_message = str(existance_exc_info.value)

    assert encode_hex(key1_shared_prefix2) in existance_exc_message
    assert encode_hex(trie_root_hash) in existance_exc_message
    assert encode_hex(first_child_hash) in existance_exc_message

    # Other keys are still accessible
    assert trie.get(key2) == b'val2'
github python-cmd2 / cmd2 / tests / test_plugin.py View on Github external
def test_postparsing_hook_undeclared_parameter_annotation():
    app = PluggedApp()
    with pytest.raises(TypeError):
        app.register_postparsing_hook(app.postparse_hook_undeclared_parameter_annotation)
github dcos / dcos / packages / dcos-launch / extra / test_config.py View on Github external
def test_error_with_installer_url(self, tmpdir):
        with pytest.raises(LauncherError) as exinfo:
            get_validated_config(
                get_temp_config_path(
                    tmpdir, 'aws-cf-with-helper.yaml', update={'installer_url': 'foobar'}))
        assert exinfo.value.error == 'ValidationError'
        assert 'installer_url' in exinfo.value.msg
        assert 'Unrecognized/incompatible' in exinfo.value.msg
github praw-dev / praw / tests / unit / models / reddit / mixins / test__init__.py View on Github external
def test_must_be_extended(self):
        with pytest.raises(NotImplementedError):
            ThingModerationMixin().send_removal_message(
                "public", "title", "message"
            )
github cuckoosandbox / cuckoo / tests / test_config.py View on Github external
def test_invalid_section():
    set_cwd(tempfile.mkdtemp())
    Folders.create(cwd(), "conf")

    Files.create(cwd("conf"), "cuckoo.conf", "[invalid_section]\nfoo = bar")
    with pytest.raises(CuckooConfigurationError) as e:
        Config("cuckoo", strict=True)
    e.match("Config section.*not found")

    Files.create(cwd("conf"), "cuckoo.conf", "[cuckoo]\ninvalid = entry")
    with pytest.raises(CuckooConfigurationError) as e:
        config("cuckoo:invalid:entry", strict=True)
    e.match("No such configuration value exists")
github falconry / falcon / tests / test_validators.py View on Github external
def test_resp_schema_validation_failure():
    with pytest.raises(falcon.HTTPInternalServerError) as excinfo:
        Resource().response_validated(GoodData(), BadData())

    assert excinfo.value.title == 'Response data failed validation'
github chainer / chainer / tests / python_tests / test_array.py View on Github external
def test_invalid_broadcast_to(src_shape, dst_shape):
    src = xchainer.ones(src_shape, xchainer.float32)
    with pytest.raises(xchainer.DimensionError):
        xchainer.broadcast_to(src, dst_shape)
github TyberiusPrime / pypipegraph / tests / test_plotjobs.py View on Github external
p1 = ppg.PlotJob("out/A.png", calc, plot)
            p2 = ppg.PlotJob("out/B.png", calc2, plot)
            import pathlib

            ppg.CombinedPlotJob(pathlib.Path("out/C.png"), [p1, p2], ["w"])
            ppg.CombinedPlotJob(pathlib.Path("out/D.png"), [p1, p2], [])
            ppg.CombinedPlotJob(
                pathlib.Path("out/E.png"),
                [p1, p2],
                {"facets": "w"},
                fiddle=lambda p: p.scale_x_log10(),
            )
            with pytest.raises(ValueError):
                ppg.CombinedPlotJob(pathlib.Path("out/C.png"), [p1, p2], "w")
            with pytest.raises(TypeError):
                ppg.CombinedPlotJob(5, [p1, p2], "w")
            with pytest.raises(ValueError):
                ppg.CombinedPlotJob("out/D.something", [p1, p2], "w")
            with pytest.raises(ValueError):
                ppg.CombinedPlotJob("out/D.png", [], "w")
            with pytest.raises(ValueError):
                ppg.CombinedPlotJob("out/D.png", [p1, p2.job_id], "w")

            ppg.run_pipegraph()
            assert magic("out/C.png").find(b"PNG image") != -1
            assert magic("out/D.png").find(b"PNG image") != -1
            assert magic("out/E.png").find(b"PNG image") != -1