from cmdtest import Program, assert_hook echo = Program('echo') @echo.test def echo_string_should_output_string(): assert echo('foo').out == 'foo\n' if __name__ == '__main__': echo.run()
from cmdtest import Program, NonExistentFile, File cat = Program('cat') @cat.test def cat_should_echo_single_file(): assert cat(File('hello')).out == 'hello' @cat.test def cat_should_combine_files(): assert cat(File('hello'), File('world')).out == 'helloworld' @cat.test def cat_should_not_write_to_stderr(): assert cat(File('hello')).err == '' @cat.test def cat_should_succeed(): assert cat(File('hello')).status == 0 @cat.test def cat_should_fail_with_bad_files(): result = cat(NonExistentFile()) assert result.err != '' assert result.status != 0 if __name__ == '__main__': cat.run()
from attest import Tests from cmdtest import Program, File head = Program('head -n10') @head.test def head_should_output_small_file(): f = File(text='hello') assert head(f).out == 'hello' @head.test def head_should_output_10_lines_of_big_file(): f = File(text='abc\n'*1000) output = head(f).out assert output.count('\n') == 10 assert output == f.text[:len(output)] if __name__ == '__main__': head.run()
from cmdtest import Program, File touch = Program('touch') @touch.test def touch_should_create_file(): touch('a_file') assert File(name='a_file').exists() if __name__ == '__main__': touch.run()
#! /usr/bin/env python from cmdtest import Program, assert_hook cut = Program('cut') @cut.test def should_select_field(): p = cut('-f', '1', _in='a\tb\n') assert p.out == 'a\n' @cut.test def should_select_multiple_fields(): p = cut('-f', '1,3', _in='a\tb\tc\n') print p.out print p.err assert p.out == 'a\tc\n' @cut.test def should_understand_delimiter(): p = cut('-f', '1', '-d', ',', _in='a,b\n') assert p.out == 'a\n' if __name__ == '__main__': cut.run()
#! /usr/bin/env python from cmdtest import Program, raises, TimeoutError sleep = Program("sleep") @sleep.test def should_eventully_raise_timeout(): with raises(TimeoutError) as error: sleep("4") @sleep.test def can_set_time_out(): assert sleep("0", _timeout=0.5).status == 0 if __name__ == "__main__": sleep.run()