Python Forum
Homework Python unit test
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework Python unit test
#1
Hi i want to passed my unit test, but i do not know how
Can you help me please
dictionary={'A':'.-', 'B':'-...',
            'C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....',
            'I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.',
            'O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-',
            'U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..',}

dictionary2={'.-':'A','-...':'B','-.-.':'C','-..':'D', '.':'E','..-.':'F','--.':'G','....':'H',
             '..':'I','.---':'J', '-.-':'K','.-..':'L', '--':'M', '-.':'N',
             '---':'O', '.--.':'P', '--.-':'Q','.-.':'R', '...':'S', '-':'T',
             '..-':'U', '...-':'V', '.--':'W','-..-':'X', '-.--':'Y', '--..':'Z',}


def coding(s):
    void=" "
    for i in s: #indexování
        if i != ' ':
            void+=dictionary[i]+" "
        else:
            void += ' '

    print(void)

    
def decoding(s):
    void = ""
    splitstring = a.split(" ") #rozdělení podle mezer
    for i in splitstring: #indexování
            void += dictionary2[i]
    print(void)
    
def selection(f):
     f=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
     return f

c = 1
d = 0
while(c!="0"):
    d =selection(d)
    a=input("ZADEJ TEXT NA ŠIFROVÁNÍ: ")
    a=a.upper()

    startUp="" # nevyužívá se ale bez ní program nelze spustit




    if d==1: # šifrování
        coding(a)



    else: # dešifrování
        decoding(a)

  
    c = input(("POKUD CHCTE PROGRAM UKONČIT, ZADEJTE 0 || PRO POKRAČOVÁNÍ ZADEJTE LIBOVOLNÝ ZNAK : "))
Unit test
from example import coding
from example import decoding
from example import selection
from example import a

import pytest

def test_coding():
    #test coding for letters A-Z
    assert test_coding('A') == '.-'
    assert test_coding('B') == '-...'
    assert test_coding('C') == '-.-.'
    assert test_coding('D') == '-..'
    assert test_coding('E') == '.'
    assert test_coding('F') == '..-.' 
    assert test_coding('G') == '--.'
    assert test_coding('H') == '....'
    assert test_coding('I') == '..'
    assert test_coding('J') == '.---'
    assert test_coding('K') == '-.-'
    assert test_coding('L') == '.-..'
    assert test_coding('M') == '--'
    assert test_coding('N') == '-.'
    assert test_coding('O') == '---'
    assert test_coding('P') == '.--.'
    assert test_coding('Q') == '--.-'
    assert test_coding('R') == '.-.'
    assert test_coding('S') == '...'
    assert test_coding('T') == '-'
    assert test_coding('U') == '..-'
    assert test_coding('V') == '...-'
    assert test_coding('W') == '.--'
    assert test_coding('X') == '-..-'
    assert test_coding('Y') == '-.--'
    assert test_coding('Z') == '--..'

    # test typeerror
    with pytest.raises(TypeError):
        test_coding(4)
        test_coding(True)
        test_coding("r")
        

    

    
def test_decoding() :
    #test decoding for letters A-Z
    assert test_decoding('.-') == 'A'
    assert test_decoding('-...') == 'B'
    assert test_decoding('-.-.') == 'C'
    assert test_decoding('-..') == 'D'
    assert test_decoding('.') == 'E'
    assert test_decoding('..-.') == 'F'
    assert test_decoding('--.') == 'G'
    assert test_decoding('....') == 'H'
    assert test_decoding('..') == 'I'
    assert test_decoding('---') == 'J'
    assert test_decoding('-.-') == 'K'
    assert test_decoding('.-..') == 'L'
    assert test_decoding('--') == 'M'
    assert test_decoding('-.') == 'N'
    assert test_decoding('---') == 'O'
    assert test_decoding('.--.') == 'P'
    assert test_decoding('--.-') == 'Q'
    assert test_decoding('.-.') == 'R'
    assert test_decoding('...') == 'S'
    assert test_decoding('-') == 'T'
    assert test_decoding('..-') == 'U'
    assert test_decoding('...-') == 'V'
    assert test_decoding('.--') == 'W'
    assert test_decoding('-..-') == 'X'
    assert test_decoding('-.--') == 'Y'
    assert test_decoding('--..') == 'Z'

def test_selection():
    assert test_selection(1) == test_coding(a)

Attached Files

.py   example.py (Size: 1.57 KB / Downloads: 106)
.py   example_test (1).py (Size: 2.46 KB / Downloads: 103)
Reply
#2
This is an error waiting to happen:
Output:
dictionary={'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..',} dictionary2={'.-':'A','-...':'B','-.-.':'C','-..':'D', '.':'E','..-.':'F','--.':'G','....':'H', '..':'I','.---':'J', '-.-':'K','.-..':'L', '--':'M', '-.':'N', '---':'O', '.--.':'P', '--.-':'Q','.-.':'R', '...':'S', '-':'T', '..-':'U', '...-':'V', '.--':'W','-..-':'X', '-.--':'Y', '--..':'Z',}
Instead writing a bunch of code that I will probably get wrong (good reason to test), I would build dictionary2 (terrible name) from dictionary (equally terrible name). Can do it in 1 line of Python code.

Your example is not written correctly for testing (or for being useful). coding() and decoding() should not print, they should return the translated string. Once modified, you test can call them with a string and compare the returned string against the expected result.

selection() will be difficult to test because it calls input(). You need to replace the existing function with one that returns strings provided by the test. I don't see this as being worth the effort.

You should modify the example.py module so it doesn't run any code when imported. Write a main() function, and only call this function if __name__ == "__main__".
Reply
#3
like this?
dictionary={'A':'.-', 'B':'-...',
            'C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....',
            'I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.',
            'O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-',
            'U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..',}

dictionary2={'.-':'A','-...':'B','-.-.':'C','-..':'D', '.':'E','..-.':'F','--.':'G','....':'H',
             '..':'I','.---':'J', '-.-':'K','.-..':'L', '--':'M', '-.':'N',
             '---':'O', '.--.':'P', '--.-':'Q','.-.':'R', '...':'S', '-':'T',
             '..-':'U', '...-':'V', '.--':'W','-..-':'X', '-.--':'Y', '--..':'Z',}


def coding(s):
    void=" "
    for i in s: #indexování
        if i != ' ':
            void+=dictionary[i]+" "
        else:
            void += ' '

    print(void)

    
def decoding(s):
    void = ""
    splitstring = a.split(" ") #rozdělení podle mezer
    for i in splitstring: #indexování
            void += dictionary2[i]
    print(void)

c = 1
while(c!="0"):
    d=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
    a=input("ZADEJ TEXT NA ŠIFROVÁNÍ: ")
    a=a.upper()

    sifra="" # nevyužívá se ale bez ní program nelze spustit




    if d==1: # šifrování
        coding(a)



    else: # dešifrování
        decoding(a)

  
    c = input(("POKUD CHCTE PROGRAM UKONČIT, ZADEJTE 0 || PRO POKRAČOVÁNÍ ZADEJTE LIBOVOLNÝ ZNAK : "))
if __name__ == "__main__
Reply
#4
No.

You need to modify coding and decoding so they return a str. They should not print. coding() should look more like encode() below. You don't need to use a list comprehension (the [to_braille.get(letter, letter) for letter in message] stuff. But I strongly suggest that you create a list of braille codes and use str.join() to convert the list to a str.
to_morse = {
    'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.',
    'H':'....', 'I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.',
    'O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-', 'U':'..-',
    'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..'}
 
def encode(message):
    """Convert message from letters to Morse code"""
    morse = [to_morse.get(letter, letter) for letter in message]
    return " ".join(morse)

def decode(message):
    """Convert message from Morse code to letters"""

if __name__ == "__main__":
    # Test functionality.  Not a unit test
    morse = encode(input("Message: "))
    print(morse)
    print(decode(morse))
Add in the decode() function and test by running the program. Once you think you have it working you can write some unit tests.

My suggestion that you wait until you think your code is working before you start writing tests probably sounds odd. After all, the purpose of testing is to find bugs. But if you start writing tests too early, you might not understand the code well enough to write effective tests.

For example, you wrote a lot of unit tests that look like this:
assert test_coding('A') == '.-'
Every one of these tests will fail for the following reasons:
1. They should call coding(), not test_coding(). test_coding() is a unit test. The unit test should call the coding() function from your module.
2. Your coding() function appends a space after each code. This may or may not be correct. As coding is currently written, coding('A') will return '._ ', not '.-'.

You need to determine if there are spaces after each code or only spaces between codes. Once you decide (and verify), then you can try some converting some messages (like SOS) to see if you have basic functionality. If you do, start writing tests.
Reply
#5
dictionary={'A':'.-', 'B':'-...',
            'C':'-.-.', 'D':'-..', 'E':'.','F':'..-.', 'G':'--.', 'H':'....',
            'I':'..', 'J':'.---', 'K':'-.-','L':'.-..', 'M':'--', 'N':'-.',
            'O':'---', 'P':'.--.', 'Q':'--.-','R':'.-.', 'S':'...', 'T':'-',
            'U':'..-', 'V':'...-', 'W':'.--','X':'-..-', 'Y':'-.--', 'Z':'--..',}
 
dictionary2={'.-':'A','-...':'B','-.-.':'C','-..':'D', '.':'E','..-.':'F','--.':'G','....':'H',
             '..':'I','.---':'J', '-.-':'K','.-..':'L', '--':'M', '-.':'N',
             '---':'O', '.--.':'P', '--.-':'Q','.-.':'R', '...':'S', '-':'T',
             '..-':'U', '...-':'V', '.--':'W','-..-':'X', '-.--':'Y', '--..':'Z',}
 
 
def coding(s):
    void=" "
    for i in s: #indexování
        if i != ' ':
            void+=dictionary[i]+" "
        else:
            void += ' '
    return void
 
     
def decoding(s):
    void = ""
    splitstring = s.split("  ")
    splitstring = s.split(" ")  # rozdělení podle mezer

    for i in splitstring:  # indexování
        void += dictionary2[i]
    return void

def main():
    c = 1
    while(c!="0"):
        d=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
        a=input("ZADEJ TEXT NA ŠIFROVÁNÍ: ")
        a=a.upper()
        if d==1: # šifrování
            print(coding(a))
        else: # dešifrování
            print(decoding(a))
    
if __name__ == "__main__":
    main()
from example import coding
from example import decoding
import pytest


def test_coding():
    assert coding('HELLO') == '.... . .-.. .-.. ---'
    assert coding('WORLD') == '.-- --- .-. .-.. -..'
    

def test_decoding():
    assert decoding('.... . .-.. .-.. ---') == 'HELLO'
    assert decoding('.-- --- .-. .-.. -..') == 'WORLD'

    
if __name__ == "__main__":
    pytest.main()
github error : https://ibb.co/0rhwvtk
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HELP in python homework makashito 4 3,933 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,966 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,313 May-09-2021, 11:19 AM
Last Post: Yoriz
  Homework with python Johnsonmfw 1 1,694 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Unit Testing is not showing Test case result mbilalshafiq 2 1,814 Jul-01-2020, 08:50 PM
Last Post: mbilalshafiq
  Python Homework Help *Urgent GS31 2 2,588 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Help with Unit Tests pdub787 3 3,068 Nov-20-2019, 07:45 PM
Last Post: ndc85430
  Python Homework Question OrcDroid123 1 2,380 Sep-01-2019, 08:44 AM
Last Post: buran
  Python homework / functions sunhyunshine 1 2,460 May-11-2019, 05:37 PM
Last Post: MrTheOne
  python homework help ASAP gk34332 1 2,987 Mar-13-2019, 07:27 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020