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 / 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:
    assert str(error.value) == "Unrecognised metric for model quality: FAIL."
github ethereum / py-trie / tests / 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:

    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 / View on Github external
def test_postparsing_hook_undeclared_parameter_annotation():
    app = PluggedApp()
    with pytest.raises(TypeError):
github dcos / dcos / packages / dcos-launch / extra / View on Github external
def test_error_with_installer_url(self, tmpdir):
        with pytest.raises(LauncherError) as exinfo:
                    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 / View on Github external
def test_must_be_extended(self):
        with pytest.raises(NotImplementedError):
                "public", "title", "message"
github cuckoosandbox / cuckoo / tests / View on Github external
def test_invalid_section():
    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 / 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 / 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 / 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], [])
                [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")

            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