def test_calls_spy(self): salary = "20m" year = 2017 #创建spy with Spy(ss.salaryService) as ss_spy: ss_spy.set_salary(salary) #调用方法 ss_spy.set_salary(salary) ss_spy.set_salary("22m") #使用calls取得调用传入的参数 #多次调用可以多次取得,calls是一个数组 assert_that(ss_spy.set_salary.calls[0].args, is_((salary, ))) assert_that(ss_spy.set_salary.calls[1].args, is_(("22m", ))) #创建spy with Spy(bs.bodyService) as bs_spy: bs_spy.get_height().returns("190cm") bs_spy.illnessHistory(year).returns("no injury") #调用方法 bs_spy.get_height() bs_spy.illnessHistory(year) #使用calls取得调用传入的参数和返回值 assert_that(bs_spy.get_height.calls[0].retval, is_("190cm")) assert_that(bs_spy.illnessHistory.calls[0].args, is_((year, ))) assert_that(bs_spy.illnessHistory.calls[0].retval, is_("no injury"))
def test_inline_stub(self): #Stub()创建free stub inline_stub_free = Stub() #使用when()设置方法参数和返回值 when(inline_stub_free).foo(1).returns("I am inline free stub") assert_that(inline_stub_free.foo(1), is_("I am inline free stub")) #Stub(Collaborator)创建stub inline_stub = Stub(bs.bodyService) # 使用when()设置方法参数和返回值 when(inline_stub).get_height().returns("188cm") assert_that(inline_stub.get_height(), is_("188cm"))
def test_delegate_stub(self): def get_height(): return "181cm" #创建stub with Stub(bs.bodyService) as stub: #使用delegates()来设定返回值,接受方法或是可以迭代的对象 stub.get_height().delegates(get_height) stub.get_weight().delegates(["120kg", "121kg"]) #验证返回值 assert_that(stub.get_height(), is_("181cm")) assert_that(stub.get_weight(), is_("120kg")) assert_that(stub.get_weight(), is_("121kg"))
def test_stub(self): playername = "Kawhi Leonard" #需使用with关键字来创建Stub #Stub接受dataService类对象作为参数,并且实现dataService类对象全部的方法 #根据dataService的实现,get_assist()等方法不用接受参数,这里的参数必须完全匹配 #get_match_number()可以根据参数的不同返回不同的值 #returns()方法定义返回值 #不能定义非dataService的属性 with Stub(ds.dataService) as stub: stub.get_assist().returns("6") stub.get_score().returns("30") stub.get_rebound().returns("10") stub.get_match_number(2015).returns(playername + " plays 80 games at the year of 2015") stub.get_match_number(2016).returns(playername + " plays 81 games at the year of 2016") #使用来自于hamcrest的assert_that()和is_()做stub的验证 assert_that(stub.get_assist(), is_("6")) assert_that(stub.get_score(), is_("30")) assert_that(stub.get_rebound(), is_("10")) assert_that(stub.get_match_number(2015), is_("Kawhi Leonard plays 80 games at the year of 2015")) assert_that(stub.get_match_number(2016), is_("Kawhi Leonard plays 81 games at the year of 2016")) #使用stub代替dataService,来对待测对象playerService进行测试验证 player_service_stub_2016 = pls.playerService(playername, 2016, stub, pos.profileService(playername), bs.bodyService(), ss.salaryService()) assert_that( player_service_stub_2016.get_player_info().split('\n')[0], is_("Kawhi Leonard - san antonio spurs")) assert_that( player_service_stub_2016.get_player_info().split('\n')[-1], is_("Kawhi Leonard plays 81 games at the year of 2016")) player_service_stub_2015 = pls.playerService(playername, 2015, stub, pos.profileService(playername), bs.bodyService(), ss.salaryService()) assert_that( player_service_stub_2015.get_player_info().split('\n')[-1], is_("Kawhi Leonard plays 80 games at the year of 2015")) #当Stub()不带参数的时候,称之为Free Stub #ANY_ARG表示任意参数 with Stub() as freestub: freestub.get_assist().returns("6") freestub.get_score().returns("30") freestub.get_rebound().returns("8") freestub.get_match_number(ANY_ARG).returns(playername + " plays 82 games") player_service_stub_2017 = pls.playerService(playername, 2017, freestub, pos.profileService(playername), bs.bodyService(), ss.salaryService()) #使用freestub代替dataService,来对待测对象playerService进行测试验证 assert_that(player_service_stub_2017.get_player_info().split('\n')[-2], is_("8")) assert_that(player_service_stub_2017.get_player_info().split('\n')[-1], is_("Kawhi Leonard plays 82 games"))
def test_observer_stub(self): def bar(): print("I am attached") with Stub() as stub: stub.foo().returns("I am foo") stub.foo.attach(bar) assert_that(stub.foo(), is_("I am foo"))
def test_adhoc_stub(self): bodyservice = bs.bodyService() #method_returning()直接在实例上建立stub,并设定返回值 bodyservice.get_height = method_returning("210cm") assert_that(bodyservice.get_height(), is_("210cm")) #method_raising()直接在实例上建立stub,并抛出异常 bodyservice.get_weight = method_raising(Exception) with self.assertRaises(Exception): bodyservice.get_weight()
def test_search_by_name(self): with Stub(Query) as stub_query: stub_query.count().returns(3) with Stub(SearchTopicsController) as stub_controller: stub_controller.filter_by_name('ingeniería').returns(stub_query) topics = stub_controller.filter_by_name('ingeniería') assert_that(topics.count(), is_(3))
def test_get_by_name(self): with Stub(TopicModel) as stub_model: stub_model.name = 'ingeniería de software' with Stub(SearchTopicsController) as stub_contoller: stub_contoller.get_by_name(ANY_ARG).returns(stub_model) topic = stub_contoller.get_by_name('ingeniería de sofware') assert_that(topic.name, is_('ingeniería de software'))
def test_get_by_title(self): with Stub(BookModel) as stub_model: stub_model.title = 'clean code' with Stub(SearchBooksController) as stub_contoller: stub_contoller.get_by_title(ANY_ARG).returns(stub_model) book = stub_contoller.get_by_title(title='clean code') assert_that(book.title, is_('clean code'))
def test_fabric_deploy_works(self): self.__prepare_file(True) fab = ProxySpy(FabricDeployer()) state.output.update({ 'status': False, 'stdout': False, 'warnings': False, 'debug': False, 'running': False, 'user': False, 'stderr': False, 'aborts': False }) tmpdir = tempfile.gettempdir() assert_that( fab.deploy(tmpdir + sep + 'fabric_deployer_test.dc'), is_(None)) assert_that(fab.deploy, called().times(1))
def test_fabric_deploy_works(self): self.__prepare_file(True) fab = ProxySpy(FabricDeployer()) state.output.update({ 'status': False, 'stdout': False, 'warnings': False, 'debug': False, 'running': False, 'user': False, 'stderr': False, 'aborts': False }) tmpdir = tempfile.gettempdir() assert_that(fab.deploy(tmpdir + sep + 'fabric_deployer_test.dc'), is_(None)) assert_that(fab.deploy, called().times(1))
def test_inline_spy(self): #Spy()创建free spy spy_inline_free = Spy() #使用when()设置方法参数和返回值 when(spy_inline_free).foo().returns("I am inline foo") #调用方法 spy_inline_free.foo() #验证调用情况 assert_that(spy_inline_free.foo(), is_("I am inline foo")) assert_that(spy_inline_free.foo, called()) #Spy()创建spy spy_inline = Spy(ss.salaryService) #使用when()设置方法参数 when(spy_inline).set_salary(ANY_ARG) #调用方法 spy_inline.set_salary("12m") #验证调用情况 assert_that(spy_inline.set_salary, called().with_args("12m"))
def test_dataframes(self, simpsons_dataset): for k, t in simpsons_dataset.tables.items(): once = simpsons_dataset.dataframes[k] twice = simpsons_dataset.dataframes[k] assert_that(once.shape, equal_to((len(t), len(t[0])))) assert_that(once.equals(twice), is_(True))