-3

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

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 https://stackoverflow.com/questions/12214801/print-a-string-as-hex-bytes

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

I need to figure out how to get the unittest to work, properly. I have an input which is encryption(x), this is called in another method. This input is needed without calling the other method to test it with a unittest. I need to test if the output equals something i already figured out beforehand, that x = 78, so i hashed out this code basically as clear as i can, english is not my first language so sorry if its bad.

Here is the newest try:

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

Also, what i expect is a test that checks weather x really equals 78. EDIT: to add i am using 2.7 python Should probably add that i use wing ide to help me spot errors with its inbuilt exception checker to help me spot what is wrong just in case it matters.

Community
  • 1
  • 1
  • ...do you have an actual *question*? – jonrsharpe Feb 28 '16 at 23:38
  • Yes, how do i actually make a unittest that works out of that jumble of stuff, because that s basically what it is. Edit: Yeah, jumble is a bit wrong. Basically i only need a unittest out of encryption(x) – HopelessNoobInPython Feb 28 '16 at 23:38
  • *"how do i actually make a unittest that works out of that jumble of stuff"* is not an appropriate question for SO, which is neither a code-writing nor tutorial service. Please take the [tour] and read [ask]. As a starter, note that it's pointless to mock out a function that the function under test *doesn't even call*. *"at least there is no errors"* - I can see several errors in your posted test code, so if you aren't seeing any traceback you must not actually be running it. – jonrsharpe Feb 28 '16 at 23:40
  • Did it dissapear? i might have clicked wrong, i need to figure out how to input 'x' as the x in ("".join("{:02x}".format(ord(c)) for c in x)) it should translate to 78, and i need to run a unittest, i figured self.assertEqual(encryption() = 78) is wrong, but basically i need to figure out a way along those lines.EDIT: I do btw have a raw_input in another part of the code that solves this for the code itself to work – HopelessNoobInPython Feb 28 '16 at 23:48
  • You posted it and it was removed - stop swearing. *"i need to figure out how to input 'x' as the x"* - so... `encryption('x')`? You really should review an introductory programming tutorial. – jonrsharpe Feb 28 '16 at 23:50
  • I really should have had a proper introductory but its my first code, the teacher figured out that since we were going the it line, we would already know enough about python so he threw that at us, make a encryption/decryption program and unittest it. encryption('x') does not work just putting it like that for some reason. I have tried inputting it in the unittest and in the program – HopelessNoobInPython Feb 28 '16 at 23:55
  • I have suggested an edit that has just been overridden by another one, so it's been rejected. @imperator, I would still have a look at it (in case you don't see it: http://stackoverflow.com/review/suggested-edits/11441957). Basically I'd suggest to cut a lot of stuff, even more than I did. Your question is full of parts explaining your situation but that aren't *really* useful. Its length can discourage others from reading it. My suggestion is to cut it drastically. Anyway, I'll leave that to you. – Fabio says Reinstate Monica Feb 29 '16 at 00:01
  • Yeah its fine. I got a basic code that i wrote up now that i believe should be somewhere along the line of what i should want. 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() – HopelessNoobInPython Feb 29 '16 at 00:02
  • 1
    **Edit the question**, to give a [mcve] and a better problem description than *"does not work"*. Otherwise, you're just wasting everyone's time. – jonrsharpe Feb 29 '16 at 00:03
  • Is that better? i am sorry. I am just getting a bit to the limit of my functions and i am kinda frustrated, i made this account twenty minutes ago since i got some hours till next i have to work. – HopelessNoobInPython Feb 29 '16 at 00:15

1 Answers1

0

Maybe you need just

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

encryption() return a string and not an integer.

Michele d'Amico
  • 22,111
  • 8
  • 69
  • 76