从__main__失败的命令行调用单元测试 [英] Command-line invocation of unittests from __main__ failing

查看:98
本文介绍了从__main__失败的命令行调用单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python解决一些练习,并使用unittest来自动化我的代码的某些验证。一个程序可以很好地运行单个单元测试,并且可以通过。第二个错误如下:

I am solving some exercises in Python and using unittest to automate some of the verification of my code. One program runs the single unittest just fine and it passes. The second gives the following error:

$ python s1c6.py
E
======================================================================
ERROR: s1c6 (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 's1c6'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

这是工作脚本的代码:

# s1c5.py
import unittest

import cryptopals


class TestRepeatingKeyXor(unittest.TestCase):
    def testCase(self):
        key = b"ICE"
        data = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
        expected = bytes.fromhex(
            "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f")
        self.assertEqual(expected, cryptopals.xorcrypt(key, data))


if __name__ == "__main__":
    unittest.main()

失败脚本的代码:

# s1c6.py
import unittest
import bitstring

import cryptopals


class TestHammingDistance(unittest.TestCase):
    def testCase(self):
        str1 = b'this is a test'
        str2 = b'wokka wokka!!!'
        expected = 37
        self.assertEqual(expected, hamming_distance(str1, str2))


def hamming_distance(str1, str2):
    temp = cryptopals.xor(str1, str2)
    return sum(bitstring.Bits(temp))


if __name__ == "__main__":
    unittest.main()

我看不到这两个程序之间的根本差异,它们会导致一个d别的。我缺少什么?

I do not see a fundamental difference between these two programs that would cause an error in one and not the other. What am I missing?

import itertools
import operator


def xor(a, b):
    return bytes(map(operator.xor, a, b))


def xorcrypt(key, cipher):
    return b''.join(xor(key, x) for x in grouper(cipher, len(key)))


def grouper(iterable, n):
    it = iter(iterable)
    group = tuple(itertools.islice(it, n))
    while group:
        yield group
        group = tuple(itertools.islice(it, n))

失败脚本的原始版本:

# s1c6_raw.py
import cryptopals

key = b"ICE"
data = b"Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
expected = bytes.fromhex(
    "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f")
print(cryptopals.xorcrypt(key, data))

上面的命令运行良好,并打印了预期的输出。

The above runs fine and prints the expected output.

推荐答案

问题是我以不同的方式运行两个脚本:

The problem is that I was running the two scripts in different ways:

$ python s1c5.py

$ python s1c6.py s1c6.txt

因为 unittest.main()解析命令行参数,在第二种情况下有错误。如果将命令行参数传递给第一个程序,我也会遇到相同的错误。

Since unittest.main() parses command line arguments, there is an error in the second case. If I pass a command line argument to the first program, I also get the same error.

这篇关于从__main__失败的命令行调用单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆