当前位置: 首页>>代码示例>>Python>>正文


Python Console.run_tests方法代码示例

本文整理汇总了Python中console.Console.run_tests方法的典型用法代码示例。如果您正苦于以下问题:Python Console.run_tests方法的具体用法?Python Console.run_tests怎么用?Python Console.run_tests使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在console.Console的用法示例。


在下文中一共展示了Console.run_tests方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_works_with_csv_files

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
    def test_works_with_csv_files(self):
        files = {'dummy.csv': 'foo,bar\nbob,alice'}
        console = Console(files=files)
        code = """
import csv
fd = open('dummy.csv')
data = csv.reader(fd.readlines())
print data.next()
print data.next()
        """.strip()
        console.run_tests(code, TEST_CODE)
        expected_output = "['foo', 'bar']\n['bob', 'alice']\n"
        self.assertEqual(expected_output, console.output)
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:15,代码来源:tests.py

示例2: test_saves_fp_on_locals

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
    def test_saves_fp_on_locals(self):
        files = {'dummy.txt': 'oi'}
        console = Console(files=files)
        code = "fd = open('dummy.txt')"
        console.run_tests(code, TEST_CODE)

        locals_ = console.get_locals()
        json_data = pickle.dumps(locals_)
        new_locals = pickle.loads(json_data)
        console = Console(locals_=new_locals)
        code = "print fd.read()"
        console.run_tests(code, TEST_CODE)
        self.assertEqual('oi\n', console.output)
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:15,代码来源:tests.py

示例3: execute_script

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
def execute_script(script, test_code, extra_locals, files):
    soft, hard = resource.getrlimit(resource.RLIMIT_CPU)
    resource.setrlimit(resource.RLIMIT_CPU, (10, hard))
    checker = Console(locals_=extra_locals, files=files)
    feedback = checker.run_tests(script, test_code)
    output = checker.output
    return_code = 0
    if feedback:
        return_code = 1
    new_locals = checker.get_locals()
    resource.setrlimit(resource.RLIMIT_CPU, (soft, hard))
    return return_code, output, feedback, new_locals
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:14,代码来源:runner.py

示例4: test_console_allows_opening_file_for_reading

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
 def test_console_allows_opening_file_for_reading(self):
     files = {'dummy.txt': u'my content\nbye'}
     console = Console(files=files)
     code = "fd = open('dummy.txt')\nprint fd.read()"
     console.run_tests(code, TEST_CODE)
     self.assertEqual(files['dummy.txt'] + '\n', console.output)
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:8,代码来源:tests.py

示例5: test_console_should_respect_locals

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
 def test_console_should_respect_locals(self):
     console = Console(locals_={'name': 'bernardo', 'idade': 25})
     code = "print 'bar' # little hack with minus: -"
     feedback = console.run_tests(code, TEST_CODE)
     self.assertEqual('', feedback)
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:7,代码来源:tests.py

示例6: TestConsoleTestsRunner

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
class TestConsoleTestsRunner(unittest.TestCase):

    def setUp(self):
        self.console = Console()

    def test_catch_syntax_error(self):
        code = "name = 'bernard"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"Teve um erro de sintaxe em algum lugar...."
        self.assertEqual(expected_msg, feedback)

    def test_catch_other_exceptions(self):
        code = "idade = 2 / 0"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"Dividir por zero não dá né..."
        self.assertEqual(expected_msg, feedback)

    def test_check_for_name_variable(self):
        code = "idade = 3"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"Você não declarou a variável name"
        self.assertEqual(expected_msg, feedback)

    def test_check_name_variable_value(self):
        code = "name = 'ronaldo'"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"Acho que o nome não ficou com o valor certo..."
        self.assertEqual(expected_msg, feedback)

    def test_check_for_idade_variable(self):
        code = "name = 'bernardo'"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"Você não declarou a variável idade"
        self.assertEqual(expected_msg, feedback)

    def test_check_idade_variable_value(self):
        code = "name='bernardo'\nidade = 13"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"A idade não está correta..."
        self.assertEqual(expected_msg, feedback)

    def test_checks_idade_was_calculated_using_subtraction(self):
        code = "name='bernardo'\nidade = 25"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"Você robou com a idade, não? Temos que fazer a subtração..."
        self.assertEqual(expected_msg, feedback)

    def test_checks_idade_was_calculated_using_subtraction(self):
        code = "name='bernardo'\nidade = 2013 - 1988\nprint 'foo'"
        feedback = self.console.run_tests(code, TEST_CODE)
        expected_msg = u"Faltou imprimir o valor 'bar'"
        self.assertEqual(expected_msg, feedback)

    def test_do_not_return_feedback_if_the_code_passes_on_the_tests(self):
        code = "name='bernardo'\nidade = 2013 - 1988\nprint 'foo'\nprint 'bar'"
        feedback = self.console.run_tests(code, TEST_CODE)
        self.assertEqual('', feedback)

    def test_console_should_respect_locals(self):
        console = Console(locals_={'name': 'bernardo', 'idade': 25})
        code = "print 'bar' # little hack with minus: -"
        feedback = console.run_tests(code, TEST_CODE)
        self.assertEqual('', feedback)

    def test_saves_code_output(self):
        code = "print 'bernardo'; print 18"
        self.console.run_tests(code, TEST_CODE)
        self.assertEqual('bernardo\n18\n', self.console.output)

    def test_console_allows_opening_file_for_reading(self):
        files = {'dummy.txt': u'my content\nbye'}
        console = Console(files=files)
        code = "fd = open('dummy.txt')\nprint fd.read()"
        console.run_tests(code, TEST_CODE)
        self.assertEqual(files['dummy.txt'] + '\n', console.output)

    def test_raises_IOError_if_file_does_not_exist(self):
        console = Console(files={})
        code = "fd = open('dummy.txt')\nprint fd.read()"
        console.run_tests(code, TEST_CODE)
        errors_msg = "[Errno 2] No such file or directory: 'dummy.txt'"
        self.assertIn(errors_msg, console.output)

    def test_accepts_writing_fiels(self):
        console = Console(files={})
        code = "fd = open('dummy.txt', 'w')\nfd.write('oi')\nfd.close()\nfd = open('dummy.txt')\nprint fd.read()"
        console.run_tests(code, TEST_CODE)
        self.assertEqual('oi\n', console.output)

    def test_saves_fp_on_locals(self):
        files = {'dummy.txt': 'oi'}
        console = Console(files=files)
        code = "fd = open('dummy.txt')"
        console.run_tests(code, TEST_CODE)

        locals_ = console.get_locals()
        json_data = pickle.dumps(locals_)
        new_locals = pickle.loads(json_data)
        console = Console(locals_=new_locals)
        code = "print fd.read()"
#.........这里部分代码省略.........
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:103,代码来源:tests.py

示例7: test_accepts_writing_fiels

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
 def test_accepts_writing_fiels(self):
     console = Console(files={})
     code = "fd = open('dummy.txt', 'w')\nfd.write('oi')\nfd.close()\nfd = open('dummy.txt')\nprint fd.read()"
     console.run_tests(code, TEST_CODE)
     self.assertEqual('oi\n', console.output)
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:7,代码来源:tests.py

示例8: test_raises_IOError_if_file_does_not_exist

# 需要导入模块: from console import Console [as 别名]
# 或者: from console.Console import run_tests [as 别名]
 def test_raises_IOError_if_file_does_not_exist(self):
     console = Console(files={})
     code = "fd = open('dummy.txt')\nprint fd.read()"
     console.run_tests(code, TEST_CODE)
     errors_msg = "[Errno 2] No such file or directory: 'dummy.txt'"
     self.assertIn(errors_msg, console.output)
开发者ID:henriquebastos,项目名称:pycursos-console,代码行数:8,代码来源:tests.py


注:本文中的console.Console.run_tests方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。