Exemplo n.º 1
0
def test_callable_example():
    def func(arg):
        if not isinstance(arg, str):
            raise TypeError()
        return arg + "yay"

    assert_that(func).when_called_with(1).raises(TypeError)
    assert_that(func).if_called_with("Hey-").returns("Hey-yay")
Exemplo n.º 2
0
def test_not_has_length():
    assert_that([1]).not_has_length(2)
    assert_that([1, 2, 3]).not_has_length(["a"])

    assert_that("Hello").not_has_length([1])

    assert_that({"a": 1}).not_has_length(2)
Exemplo n.º 3
0
def test_readme_examples():
    assert_that("str").not_equals("string")
    assert_that(5).is_in(range(10))

    def func(arg):
        if not isinstance(arg, str):
            raise TypeError()
        return arg + "yay"

    assert_that(func).if_called_with(1).raises(TypeError)
    assert_that(func).if_called_with("Hey-").returns("Hey-yay")
Exemplo n.º 4
0
def test_has_attribute():
    assert_that("str").has_attribute("__str__")

    class MyClass:
        name = "MyClass"

    assert_that("str").has_attribute("__str__")
    assert_that(MyClass()).has_attribute("name")
Exemplo n.º 5
0
def test_equals_with_same_class():
    class Tmp:
        def __init__(self, var):
            self.var = var

        def __eq__(self, other):
            return self.var == other.var

    t1 = Tmp(1)
    t2 = Tmp(2)
    t3 = Tmp(1)
    assert_that(t1).equals(t1)
    assert_that(t1).equals(t3)
    assert_that(t1).not_equals(t2)
    assert_that(t3).not_equals(t2)
Exemplo n.º 6
0
def test_when_called_with_raises():
    def razor(arg):
        if not isinstance(arg, str):
            raise TypeError()

    assert_that(razor).when_called_with(1).raises(TypeError)
    assert_that(razor).if_called_with(1).raises(TypeError)
    try:
        assert_that(razor).if_called_with("str").raises(TypeError)
        assert False, "did not raise exception"
    except AssertionError:
        pass
Exemplo n.º 7
0
def test_equals_with_dict():
    a = {"a": 1}
    b = {"a": 1}
    assert_that(a).equals(b)
    assert_that(a).not_equals({"c": 2})
Exemplo n.º 8
0
def test_equals_int():
    assert_that(1).equals(1)
    assert_that(2).not_equals(3)
Exemplo n.º 9
0
def test_has_attribute_with_value():
    class MyClass:
        name = "MyClass"

    assert_that(MyClass()).has_attribute_with_value("name", "MyClass")
Exemplo n.º 10
0
def test_is_true():
    assert_that(True).is_true()
    assert_that(1 == 1).is_true()
Exemplo n.º 11
0
def test_is_not_false():
    assert_that(True).is_not_false()
Exemplo n.º 12
0
def test_contains_key_with_value():
    assert_that({"a": 1}).contains_key_with_value("a", 1)
Exemplo n.º 13
0
def test_is_subset_of(subset, superset):
    assert_that(subset).is_subset_of(superset)
Exemplo n.º 14
0
def test_has_length_on_list():
    assert_that([]).has_length(0)
    assert_that([1]).has_length(1)
    assert_that([1, 2]).has_length([2, 5])

    assert_that("str").has_length(3)

    assert_that({}).has_length(0)
    assert_that({"a": 1}).has_length(1)
    assert_that({"a": 1}).has_length({"b": 2})
    assert_that({"a": 1}).not_has_length(2)
Exemplo n.º 15
0
def test_not_contains():
    assert_that([1, 2, 3]).not_contains(4)
    assert_that({}).not_contains(4)
Exemplo n.º 16
0
def test_has_length_greater():
    assert_that(["a", "b"]).has_length_greater_than(1)
    assert_that(["a", "b"]).has_length_greater_or_equal_to(2)
    assert_that(["a", "b"]).has_length_greater_or_equal_to(0)

    assert_that(["a", "b", "c"]).has_length_greater_or_equal_to([1])

    assert_that({"a": 1, "b": 2}).has_length_greater_than(1)
    assert_that({"a": 1, "b": 2}).has_length_greater_or_equal_to(2)
Exemplo n.º 17
0
def test_equals_with_list():
    a = [1]
    b = [1]
    assert_that(a).equals(b)
    assert_that(a).not_equals([3])
Exemplo n.º 18
0
def test_equals_str():
    assert_that("str").equals("str")
    assert_that("str").not_equals("yo")
Exemplo n.º 19
0
def test_is_not_true():
    assert_that(False).is_not_true()
Exemplo n.º 20
0
def test_equals_float():
    assert_that(1.2).equals(1.2)
    assert_that(0.1).not_equals(-0.1)
Exemplo n.º 21
0
def test_has_length_less():
    assert_that([1, 2, 3]).has_length_less_than(4)
    assert_that([1, 2, 3]).has_length_less_or_equal_to(3)
    assert_that([1, 2, 3]).has_length_less_or_equal_to(5)
Exemplo n.º 22
0
def test_is_none():
    assert_that(None).is_none()
    assert_that("str").is_not_none()
Exemplo n.º 23
0
def test_contains():
    assert_that([1, 2, 3]).contains(1)
    assert_that({"a": 1}).contains("a")
Exemplo n.º 24
0
def test_has_type_on_str():
    assert_that("str").has_type(str)
    assert_that("str").has_type("string")
    assert_that("str").not_has_type(int)
    assert_that("str").not_has_same_type_as(int)
Exemplo n.º 25
0
def test_contains_key():
    assert_that({"a": 1}).contains_key("a")
    assert_that({"a": 1}).not_contains_key("b")
Exemplo n.º 26
0
def test_is_in():
    assert_that(1).is_in(range(10))
    assert_that(10).is_not_in(range(10))
Exemplo n.º 27
0
def test_contains_subset(superset, subset):
    assert_that(superset).contains_subset(subset)
Exemplo n.º 28
0
def test_greater():
    assert_that(1).is_greater_than(0)
    assert_that(1).is_greater_or_equal_to(0)
    assert_that(1).is_greater_or_equal_to(1)
Exemplo n.º 29
0
def test_has_same_elements_as():
    assert_that([1, 2, 3]).has_same_elements_as([2, 1, 3])
    assert_that("gnirts").has_same_elements_as("string")
Exemplo n.º 30
0
def test_less():
    assert_that(1).is_less_than(2)
    assert_that(1).is_less_or_equal_to(1)
    assert_that(1).is_less_or_equal_to(2)