Ash
Ash

Reputation: 1

How do i do unit tests in python?

I have a simple program for traslating morse code back and forth as a school project and part of it is also writting unit tests. I ve tried for multiple hours but was not succesful at all

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: #indexing
        if i != ' ':
            void+=dictionary[i]+' '
        else:
            void +=' '
    
    print(void)

    
def decoding(s):
    void = ""
    splitstring=s.split("  ")
    
    splitstring = s.split(" ")#splitting by spaces
    #splitstring=splitstring.remove('')

    
    for i in splitstring: #indexing
        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="" # not being used but program cant be ran without it for some reason




    if d==1: # translating to morse code
        coding(a)   
        


    else: # translating from morse code    
         decoding(a)

  
    c = input(("POKUD CHCTE PROGRAM UKONČIT, ZADEJTE 0 || PRO POKRAČOVÁNÍ ZADEJTE LIBOVOLNÝ ZNAK : "))

this is what i ve tried:

import pytest
from example import coding
from example import decoding



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

#To write unit tests for the decoding() function, we can test that it decodes strings correctly by comparing the output of the function with the expected result. For example:


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

test_decoding()

My teacher wants those tests to be ran with this command:

py.exe -m pytest --cov=. --cov-fail-under=66

in command block, it has to reach at least 66% success rate.

thus far i ve always ended up with this result:

=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.9.6, pytest-7.2.0, pluggy-1.0.0
rootdir: C:\Users\ash31\OneDrive\Plocha\AP1VS-final-project
plugins: cov-4.0.0
collected 0 items / 1 error

================================================================================================================== ERRORS ==================================================================================================================
_____________________________________________________________________________________________________ ERROR collecting example_test.py _____________________________________________________________________________________________________
example_test.py:1: in <module>
    from example import coding
example.py:51: in <module>
    d =selection(d)
example.py:43: in selection
    f=int(input("1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY ")) # menu
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\_pytest\capture.py:192: in read
    raise OSError(
E   OSError: pytest: reading from stdin while output is captured!  Consider using `-s`.
------------------------------------------------------------------------------------------------------------- Captured stdout --------------------------------------------------------------------------------------------------------------
1. Z ČEŠTINY DO MORSEOVKY || 2. Z MORESOVKY DO ČEŠTINY

----------- coverage: platform win32, python 3.9.6-final-0 -----------
Name              Stmts   Miss  Cover
-------------------------------------
example.py           32     22    31%
example_test.py      24     23     4%
-------------------------------------
TOTAL                56     45    20%

FAIL Required test coverage of 66% not reached. Total coverage: 19.64%
========================================================================================================= short test summary info ==========================================================================================================
ERROR example_test.py - OSError: pytest: reading from stdin while output is captured!  Consider using `-s`.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================= 1 error in 0.46s =============================================================================================================

Upvotes: 0

Views: 51

Answers (0)

Related Questions