dimanche 28 février 2016

Unittest of an encryption code

Ok, so i believe i am missing something really obvious, but i have been trying for a long time to figure out a way and everyone who has tried to help me just tell me that i pretty much got it set up correctly for everything to work, but it doesn't no matter what test i try, i have been through so many but the most promising for now is

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')

at least there is no errors, but i don't know where to go from now and i don't really get a measure on what works or how it works. I stole this from python mocking raw input in unittests I just don't understand how it works, at all...

The code that i want to test is

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 http://ift.tt/1GkMMVC

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

I gutted my code to get this, but that should be the most important things for my question. I want to unittest encryption(x), which basically means i want to test if x = 78 (or anything else is their corresponding hexadecimals, i don't really care as long as the answer is what i expected-.-), i also have to use the unittest package because school reasons... I believe this has been answered before, but because i can't comment due to lack of reputation i can't really ask when i don't understand so i had to make a new question.

I believe the worst part of this issue is that feel that i understand far less then before i started this, ask me to write a simple (code simple with lots of if else) game, sure i will do that, ask me to make a website, yeah sure, but testing that stuff is fucking horrible... I never had any idea that unittests were such a hassle before now, and i am really put off by them. I do have a unittest that works, and the guy who wrote it shrugged and said "meh, it just works. I have no idea how" The expert on coding in class kept telling me that my original code was impossible to test, and insisted that this code is really easy to test but basically haven't answered me properly for two weeks now on how to actually write the test.

I am probably just retarded for not seeing it :( EDIT: to add i am using 2.7 python

Aucun commentaire:

Enregistrer un commentaire