Package: the
python better assertion.inspired by should.js and chai.jshttp://the-py.github.io/the/tested on python2.7, 3.2, 3.3installpipinstalltheAPIIt provides one object calledthewith an aliasexpect. (Take a
look at the Usage and Example.)fromtheimportthe,expectChainsdo nothing but return itself.shouldhavetowhenbe. Special. You can use it as a chain but it also plays as a
matcher.a. Special. You can use it as a chain but it also plays as a
matcher.More chains?the.use("mychain")Matchers without argtrigger a certain assertion.true. assert Truefalse. assert Falsenone. assert Noneexist. assert not Noneok. assert Truthyempty. assert FalsyMore?take a look atthe-easytypelib.# define your matcherdefhappy(self):returnself._check(self.obj=="happy",self.obj+" is happy.",self.obj+" is not happy.")# add to `the`the.use(happy)# DONE!the(string).should.be.happyMatchers with argstrigger a certain assertioneq(other),equal(other). assert equal(==) @param: other
{mixed}lt(other),below(other). assert less than(<) @param: other
{mixed}gt(other),above(other). assert greater than(<) @param: other
{mixed}ne(other). assert not equal(!=) @param: other {mixed}le(other). assert less than or equal to (>=). @param: other
{mixed}ge(other). assert greater than(>=). @param: other {mixed}match(regex). assert string match a regex. @param: regex {mixed}length(n),size(n). assert length. @param: n {int}item(**kwargs),items(**kwargs). assert dict have item(s).
@params: **kwargscontain(other). assert a dict contains another dict. @param:
other {dict}key(*args),keys(*args). assert dict has key(s). @params:
argsvalue(*args),values(*args). assert dict has value(s).
@params: argsproperty(**kwargs),properties(**kwargs). assert object has
property/properties. @params: argsinclude(item). assert container include item (in). @param: item
{mixed}within(container). assert item in container (in). @param:
container {mixed}inherit(parent). assert subclass. @param: parent {class}method(m). assert object has method. @param: m {str}result(r). assert function return value. Usingapplyto apply
args @param: r {mixed}throw(msg=None, ex=Exception). assert function throw exception
usingapplyto apply args @param: msg {regex} optional @param: ex
{exception} optionalbe(other). assertis. (It can also be used as a chain)
@param: other {mixed}.a(cls). assertisinstance. (It can also be used as a chain)
@param: cls {class}exception(msg=None, ex=Exception)assert exception throwclassmethod@param: msg {regex} optional @param: ex {exception}
optionalMore?take a look atthe-fslib.# define your matcherdeffirstname(self,name):fname=self.obj.split()[0]returnself._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(firstname)# DONE!expect("Wenjun Yan").to.have.firstname("Wenjun")Magic methods==. e.g.expect(1) == 1!=. e.g.expect(1) != 2>=. e.g.expect(1) >= 1<=. e.g.expect(2) <= 3>. e.g.expect(3) > 2<. e.g.expect(4) < 5in. e.g.1  in the(range(1,3))[]. e.g.the(dictionary)["key"]== "value"NegationsNOTnot_toshould_notPluginuse(*args,**kwags). use this to extendthefunctionality.classmethod@param: *args @param: **kwargsargscan be a
string (which will become a new chain), method(new matcher), list of arg
or a dict (in this casethewill use the key as new matcher’s name.
Same askwargs.).argscan even be a module if it provides aAPIvariable containing all matchers and chains to export.Usage and Examplesassert>,<,>=,<=,==expect(1)>0expect(1).gt(0)expect(1).above(0)expect(1)>=0expect(1).ge(0)expect(1)<2expect(1).lt(0)expect(1).below(0)expect(1)<=2expect(1).le(0)expect(1)==1expect(1).eq(1)expect(1).equal(1)assertTrue,False,Nonethe(True).should.be.trueexpect(True).to.be.truethe(False).should.be.falseexpect(False).to.be.falsethe(None).should.be.noneexpect(None).to.be.noneasserttruthy,falsythe(1).should.be.okexpect(1).to.be.okthe("").should.be.emptyexpect("").to.be.emptyassertisthe(1).should.be(1)expect(1).to.be(1)assertisinstancethe(1).should.be.an(int)expect("1").to.be.a(str)assertissubclassthe(int).should.inherit(object)expect(int).to.inherit(object)assertinthe(1).should.be.within(range(1,3))expect(1).to.be.within(range(1,3))assertlenthe(range(1,3)).should.have.length(3)expect(range(1,3)).to.have.length(3)assertregexpthe("abc").should.match("a")expect("abc").to.match("a")assertdict.itemd={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"]==1the(d).should.contain({"a":1,"b":2})expect(d).to.contain({"a":1,"b":2})assertdict.keyd={a:1,b:2}the(d).should.have.key("a")expect(d).to.have.keys("a","b")assertdict.valued={a:1,b:2}the(d).should.have.value(1)expect(d).to.have.values(1,2)assertpropertyclassA(object):def__init__(self):self.x=1defgetx(self):returnself.xexpect(A()).to.have.property("x")expect(A()).to.have.property(x=1)assertmethodclassA(object):def__init__(self):self.x=1defgetx(self):returnself.xexpect(A()).to.have.method("getx")the(A()).should.have.method("getx")assertfunctiondefdiv(a,b):returna/bexpect(div).when.apply(1,2).to.have.result(1/2)expect(div).when.apply(1,0).to.throw()assertexceptionwithexpect.exception():assert1==2Pluginthe-fsUseosto assert
file system .the-easytype. Another
way to assert basic types.