Python单元测试 - 如何摆脱测试?

时间:2017-12-05 09:44:12

标签: python pycharm

Click here to see picture

我正在编写一个简单的Python程序,但出于某种原因,它运行了奇怪的测试,而不是运行程序本身。我该怎么办? 我正在使用Jetbrain的Pycharm ...... 我的想法是让一个python程序运行一个名为Gvahinux的组合语言的文件,它类似于汇编程序,简单得多......

那么如何摆脱自动运行的Unittests?

我打开或写的任何新程序都会发生这种情况。 pycharm在程序上运行测试而不是简单地运行程序会发生什么?

以下是代码:

cpu = dict(REGS = {             ' R0':0,             ' R1':0,             ' R2':0,             ' R3':0,             ' R4':0,             ' R5':0,             ' R6':0,             ' R7':0,             ' PC':0,             ' CC':0         })

    def cpu_execute_line(line):
        parameter_1 = split_line(line)[1]
        parameter_2 = split_line(line)[2]
        command = commands[split_line(line)[0]]
        command(parameter_1, parameter_2)


    def mov(rx, op):
        try:
            if int(op):
                cpu['REGS'][rx] = int(op)
        except ValueError:
            cpu['REGS'][rx] = cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def add(rx, op):
        try:
            if int(op):
                cpu['REGS'][rx] += int(op)
        except ValueError:
            cpu['REGS'][rx] += cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def sub(rx, op):
        try:
            if int(op):
                cpu['REGS'][rx] -= int(op)
        except ValueError:
            cpu['REGS'][rx] -= cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def mul(rx, op):
        try:
            if int(op):
                cpu['REGS'][rx] *= int(op)
        except ValueError:
            cpu['REGS'][rx] *= cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def div(rx, op):
        try:
            if int(op):
                cpu['REGS'][rx] /= int(op)
        except ValueError:
            cpu['REGS'][rx] /= cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def mod(rx, op):
        try:
            if int(op):
                cpu['REGS'][rx] %= int(op)
        except ValueError:
            cpu['REGS'][rx] %= cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def comp(rx, op):
        try:
            if int(op):
                cpu['REGS']['CC'] = int(cpu['REGS'][rx]) - int(op)
        except ValueError:
            cpu['REGS']['CC'] = cpu['REGS'][rx] - cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def test(cond):
        if cond == 'EQ':
            if cpu['REGS']['CC'] == 0:
                return True
        elif cond == 'NE':
            if cpu['REGS']['CC'] != 0:
                return True
        elif cond == 'HI':
            if cpu['REGS']['CC'] > 0:
                return True
        elif cond == 'LO':
            if cpu['REGS']['CC'] < 0:
                return True
        elif cond == 'HE':
            if cpu['REGS']['CC'] >= 0:
                return True
        elif cond == 'LE':
            if cpu['REGS']['CC'] <= 0:
                return True
        elif cond == 'AL':
            return True
        return False


    def jmp(cond, op):
        if test(cond):
            try:
                if int(op):
                    cpu['REGS']['PC'] = int(op)
            except ValueError:
                cpu['REGS']['PC'] = cpu['REGS'][op]
        else:
            cpu['REGS']['PC'] += 1


    def out(cond, op):
        if test(cond):
            try:
                if int(op):
                    print op
            except ValueError:
                print cpu['REGS'][op]
        cpu['REGS']['PC'] += 1


    def split_line(line):
        code_part = line.split("#")[0]  # in order to get rid of the comments'  MOV    R1   ,   0      #puts in reg
        if code_part.split():
            code_part = code_part.split()
            command = code_part.pop(0)
            code_part = "".join(code_part)
            code_part = code_part.split(',')
            return command, code_part[0], code_part[1]


    commands = {'MOV': mov,
                'ADD': add,
                'SUB': sub,
                'MUL': mul,
                'DIV': div,
                'MOD': mod,
                'CMP': comp,
                'JMP': jmp,
                'OUT': out
                }


    def execute_program(lines):
        while (cpu['REGS']['PC']) < lines.__len__():
            cpu_execute_line(lines[cpu['REGS']['PC']])


    if __name__ == '__main__':
        path = 'C:\Users\Banana\PycharmProjects\untitled1\\fib.gvahinux'
        program = open(path, "r")
        program = program.readlines()
        execute_program(program)
        print (cpu['REGS'])

0 个答案:

没有答案