0

I am very new to python and have been tasked with using unittest for my code,

my code is as follows:

import platform
import os

OpSys=platform.system()

def clear ():
    if (OpSys=="Windows"):
        os.system("cls")
    else:
        os.system("clear")

def ShowPlaca(type):
    if (type!=""):
        print("\nLa placa pertenece a", type)
    else:
        print("\nPlaca no reconocida")

class Placa:

    def TypePlaca(self):
        placa=input("\nIngrese una placa: ")
        if re.match("^([A-Z]|[0-9]){6}$", placa):
            print("\nIngresaste una placa")

            if re.match("^MA([A-Z]|[0-9]){4}$", placa):
                type=("una motocicleta")

            elif re.match("^MB([A-Z]|[0-9]){4}$", placa):
                type=("un MetroBus")

            elif re.match("^TA([A-Z]|[0-9]){4}$", placa):
                type=("un taxi")

            elif re.match("^E([A-Z]{1}|[0-9])+", placa):
                type=("un vehiculo fiscal o judicial")

            elif re.match("^CP([A-Z]|[0-9])+", placa):
                type=("un vehiculo del canal")

            elif re.match("^BU( |[0-9]{4})+$", placa):
                type=("un Bus")

            elif re.match("^HP( |[0-9]{4})+$", placa):
                type=("un radioaficionado")

            elif re.match("^A([A-Z]{1}|[0-9]{4})+$", placa):
                type=("un auto regular")

            elif re.match("^CC([A-Z]|[0-9])+$", placa):
                type=("un cuerpo consular")

            elif re.match("[0-9]+$", placa):
                type=("una serie antigua")

            elif re.match("^PR([A-Z]|[0-9])+", placa):
                type=("un auto de prensa")

            else:
                type=("")
            ShowPlaca(type)

        else:
            print ("Placa no valida")

placa=Placa()


if __name__=="__main__":
    n=0
    while n==0:

        print(" \n Las placas siguen este formato:")
        print("\n\nMoto:            MA####\nAuto Regular:    A(A-G)###")
        print("Autos de prensa: PR####\nCuerpo Consular: CC####")
        print("Radioaficionado: HP####\nBus:             BU####")
        print("Fiscal :         E(A-Z)####\nTaxi:            TA####\nMetroBus:        MB####")
        print("Las placas antiguas se conforman de 6 numeros.")
        print("\n")


        placa.TypePlaca()
        print ("\nPresione 1 para hacer otra consulta o 2 para salir")
        x = input ()
        if x == '2':
            n += 1
        elif x == '1':
            clear ()
    exit ()

Please excuse the Spanish, this is how I have been trying to test it:

UPDATED

import unittest
from placas2 import TypePlaca


class Testplacas2(unittest.TestCase):

    def test_TypePlaca(self):
        placa = "123456"
        self.assertEqual(placa.TypePlaca ,"una serie antigua...")


if __name__ == '__main__':
    unittest.main()

When I try to run it gives me the following error:

Traceback (most recent call last):
  File "C:\Users\movid\Documents\Programacion 3\Final\source\test_placas2.py", line 2, in <module>
    from placas2 import TypePlaca
ImportError: cannot import name 'TypePlaca' from 'placas2' (C:\Users\movid\Documents\Programacion 3\Final\source\placas2.py)

To make things clear I have to test the 10 different types of car plates and see if that would give me the given result, while operating the program if my input is 123456, it does return "una serie antigua..." but most likely I need help structuring my test differently to get the result Im looking for.

Fabs123
  • 1
  • 1
  • What is the error? You should share it, too. – Cagri Dec 14 '20 at 03:24
  • right now I am facing an import error:' ImportError: cannot import name 'TypePlaca' from 'placas2' (C:\Users\movid\Documents\Programacion 3\Final\source\placas2.py)' – Fabs123 Dec 14 '20 at 03:45
  • I couldn't find `placas2` library or `TypePlaca` module online. It seems like you are importing from a local file. Check this question to see if you are doing any import mistake https://stackoverflow.com/questions/4142151/how-to-import-the-class-within-the-same-directory-or-sub-directory. – Cagri Dec 14 '20 at 03:56
  • I keep checking and doing everything on that link but its no different, it is importing form a local file which is also on the same directory – Fabs123 Dec 14 '20 at 04:09
  • Is the code you provided at the beginning of your question `test_placas2.py` or is it some else code? Maybe try to copy `from placas2 import TypePlaca` to that file. – Cagri Dec 14 '20 at 04:31
  • it is the same code I provided at the beginning yes, placas2 is the name of the file that contains that code – Fabs123 Dec 14 '20 at 04:50
  • Again, I have to refer you to the question that I sent the link in my previous comment. That definitely looks like an import issue. Check every response to the question. https://stackoverflow.com/questions/4142151/how-to-import-the-class-within-the-same-directory-or-sub-directory. I am sorry I can't help more than that – Cagri Dec 14 '20 at 05:34

0 Answers0