chara使开发人员能够快速创建特性测试。首先使用您喜欢的任何测试框架创建集成测试。使用chara可以修饰这些测试函数来记录具有特定依赖项的交互。之后,您可以重放记录,这些依赖项将按照记录进行操作。
chara的Python项目详细描述
#字符
chara使开发人员能够快速创建特性测试。首先使用您喜欢的任何测试框架创建集成测试。使用chara可以修饰这些测试函数来记录具有特定依赖项的交互。之后,您可以重放录制,这些依赖项将表现为录制。
##用法
创建生产代码并通过集成测试(test_scraper.py):
def scrape_headline(name): url, search = { 'wired': ( 'http://www.wired.com', '<div class="headline headline1">\s*' '<h5>[^<]*</h5>\s*' '<h2>\s*<a[^>]*>([^<]*?)</a>\s*</h2>\s*' '</div>' ) }[name] matches = re.search(search, get_html(url)) return matches.group(1) if matches else None def get_html(url): return urllib2.urlopen(url).read() class DemoTest(TestCase): def test_scraper(self): self.assertEqual( 'This Is What Happens When You Hack and ' 'Extort the \'Bitcoin Jesus\'', scrape_headline('wired') )
添加chara.record decorator并运行测试:
class DemoTest(TestCase): @chara.record('test_scraper.get_html') def test_scraper(self): self.assertEqual( 'This Is What Happens When You Hack and ' 'Extort the \'Bitcoin Jesus\'', scrape_headline('wired') )
切换到使用chara.replay并注意,即使将get_html()转换为no op函数,测试仍会通过,因此您知道chara的replay功能正在绕过函数的正常功能。:
def get_html(url): pass # nothing even happening here, but the test passes! class DemoTest(TestCase): @chara.replay('test_scraper.get_html') def test_scraper(self): self.assertEqual( 'This Is What Happens When You Hack and ' 'Extort the \'Bitcoin Jesus\'', scrape_headline('wired') )
##许可证
使用[mit](http://opensource.org/licenses/MIT)许可证。