View on GitHub

The

chai.js/should.js like python lib.

download .ZIPdownload .TGZ

python better assertion.

inspired by should.js and chai.js

install

pip install the

API

It provides one object called the with an alias expect. (Take a look at the Usage and Example.)

from the import the, expect

Chains

do nothing but return itself.

More chains?
the.use("mychain")

Matchers without arg

trigger a certain assertion.

More?

take a look at the-easytype lib.

# define your matcher
def happy(self):
    return self._check(self.obj == "happy",
                       self.obj + " is happy.",
                       self.obj + " is not happy.")

# add to `the`
the.use(happy)

# DONE!
the(string).should.be.happy

Matchers with args

trigger a certain assertion

More?

take a look at the-fs lib.

# define your matcher
def firstname(self, name):
      fname = self.obj.split()[0]
      return self._check(fname == name,
                         "The firstname of {} is {}".format(self.obj, name),
                         "The firstname of {} is not {}".format(self.obj, name))

# add to `the`
the.use(firname)

# DONE!
expect("Wenjun Yan").to.have.firstname("Wenjun")

Magic methods

Negations

Plugin

use(*args, **kwags). use this to extend the functionality. classmethod
@param: args
@param: *
kwargs
args can be a string (which will become a new chain), method(new matcher), list of arg or a dict (in this case the will use the key as new matcher's name. Same as kwargs.). args can even be a module if it provides a API variable containing all matchers and chains to export.

Usage and Examples

assert >, <, >=, <=, ==

expect(1) > 0
expect(1).gt(0)
expect(1).above(0)

expect(1) >= 0
expect(1).ge(0)

expect(1) < 2
expect(1).lt(0)
expect(1).below(0)

expect(1) <= 2
expect(1).le(0)

expect(1) == 1
expect(1).eq(1)
expect(1).equal(1)

assert True, False, None

the(True).should.be.true
expect(True).to.be.true

the(False).should.be.false
expect(False).to.be.false

the(None).should.be.none
expect(None).to.be.none

assert truthy, falsy

the(1).should.be.ok
expect(1).to.be.ok

the("").should.be.empty
expect("").to.be.empty

assert is

the(1).should.be(1)
expect(1).to.be(1)

assert isinstance

the(1).should.be.an(int)
expect("1").to.be.a(str)

assert issubclass

the(int).should.inherit(object)
expect(int).to.inherit(object)

assert in

the(1).should.be.within(range(1,3))
expect(1).to.be.within(range(1,3))

assert len

the(range(1, 3)).should.have.length(3)
expect(range(1, 3)).to.have.length(3)

assert regexp

the("abc").should.match("a")
expect("abc").to.match("a")

assert dict.item

d = {a: 1, b: 2}
the(d).should.have.items(a=1, b=2)
expect(d).to.have.items(a=1, b=2)
expect(d)["a"] == 1

the(d).should.contain({"a": 1, "b": 2})
expect(d).to.contain({"a": 1, "b": 2})

assert dict.key

d = {a: 1, b: 2}
the(d).should.have.key("a")
expect(d).to.have.keys("a", "b")

assert dict.value

d = {a: 1, b: 2}
the(d).should.have.value(1)
expect(d).to.have.values(1, 2)

assert property

class A(object):
    def __init__(self):
        self.x = 1

    def getx(self):
        return self.x

expect(A()).to.have.property("x")
expect(A()).to.have.property(x=1)

assert method

class A(object):
    def __init__(self):
        self.x = 1

    def getx(self):
        return self.x

expect(A()).to.have.method("getx")
the(A()).should.have.method("getx")

assert function

def div(a, b):
    return a/b

expect(div).when.apply(1,2).to.have.result(1/2)
expect(div).when.apply(1,0).to.throw()

assert exception

with expect.exception():
    assert 1 == 2

Plugin