2016-02-28 128 views
-3

好的,所以我相信我錯過了一些非常明顯的東西,但我一直在嘗試很長時間來找出一種方法,每個人都試圖幫助我,只是告訴我,我幾乎得到了它設置了正確的一切工作,但它不會不管我考什麼,我一直在經歷了那麼多,但最有前途的,現在是單元測試的加密代碼

import unittest 
from unittest import TestCase 
from mock import patch 
from encrdecrprog import encryption 
class teststuff(TestCase): 
    def test_encryption(self): 
     with patch('__bulletin__.raw_input', return_value = 'x') as raw_input: 
      self.assertEqual(encryption(x), '78') 
      _raw_input.assert_called_once_with('x') 

我偷了這個從python mocking raw input in unittests我只是不」瞭解它是如何工作的,完全...

我想測試的代碼是

def enprint(): 

    print(encryption(raw_input())) 
def encryption(x): 

    pur = ("".join("{:02x}".format(ord(c)) for c in x)) 
    #The code for this was shamelessly stolen from https://stackoverflow.com/questions/12214801/print-a-string-as-hex-bytes 

    return pur 
def main(): 
    userinput = raw_input()  
    if userinput == "1": 
     enprint()  

我需要弄清楚如何讓單元測試正常工作。我有一個輸入是加密(x),這是用另一種方法調用的。這個輸入是不需要調用其他方法來用unittest測試的。我需要測試一下輸出是否等於事先已經知道的東西,那就是x = 78,所以我基本上儘可能的清楚了這段代碼,英文不是我的第一語言,所以如果它不好,我會很抱歉。

這裏是最新嘗試:

import unittest 
from encrdecrprog import encryption 

class TestStringMethods(unittest.TestCase): 
     def setup(self): 
       pass 
     def test_encryption(self): 
       self.assertEquals(encryption('x'), 78) 
       print self.test_encryption 

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

而且,我所期望的是一個測試,檢查天氣X真的等於78 編輯:添加我使用2.7蟒蛇也許應該補充一點,我使用翼IDE幫助我通過內置的異常檢查器發現錯誤,以幫助我發現錯誤以防萬一。

+0

...你有一個真正的*問題*? – jonrsharpe

+0

是的,我該如何做一個單元測試,這個混亂的東西,因爲它基本上是什麼。編輯:是的,混亂有點不對。基本上我只需要一個加密單元測試(x) – HopelessNoobInPython

+0

*「我如何做一個單元測試可以解決那些混亂的問題」*對於SO來說不是一個合適的問題,它既不是代碼編寫也不是教程服務。請參加[遊覽]並閱讀[問]。作爲初學者,請注意,模擬被測函數*甚至不會調用*的函數是毫無意義的。 *「至少沒有錯誤」* - 我可以在您發佈的測試代碼中看到幾個錯誤,所以如果您沒有看到任何回溯,則不得實際運行它。 – jonrsharpe

回答

0

也許你只需要

self.assertEquals(encryption('x'), "78") 

encryption()返回一個字符串,而不是一個整數。