NOVINKA - Online rekvalifikační kurz Python programátor. Oblíbená a studenty ověřená rekvalifikace - nyní i online.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.
Avatar
Paragoon2
Člen
Avatar
Paragoon2:10.12.2022 19:57

Potřeboval bych pomoct s projitím unit testu

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 : "))

Zkusil jsem:

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)

Chci docílit: Projití testu 66%

 
Odpovědět
10.12.2022 19:57
Avatar
Pavel
Člen
Avatar
Pavel:12.12.2022 16:12
  1. abys mohl něco testovat, tak to musí vracet nějakou hodnotu, print nevrací nic
  2. musíš zamezit spouštění toho while cyklu v testu, např. vložením do if __name__ == '__main__':
  3. musíš testovat to co importuješ, tedy coding a ne test_coding, která žádné argumenty nebere
  4. ta sifra fakt není třeba, asi máš někde nějaký bordel
  5. ty for cykly lze nahradit přímo voláním dictionary.get(klic, default_kdyz_ne­najde)'
  6. importuješ selection, který ale nikde v není
 
Nahoru Odpovědět
12.12.2022 16:12
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 2 zpráv z 2.