1

I'm practising with unit test module.

I have this simple code. It is a function that joins strings so that I can get this result: "City, Country"

def city_country(city_name, country_name):

    """Simple code that prints City, Country"""

    print(city_name.title() + ", " country_name.title())

When I run it, the function works OK.

I wrote a class to test the code with unit tests, and I got an error.

I noticed that when I assign the function to a variable, like this :

city_country_var = city_country('Punto Fijo', 'Venezuela') 

And then import it to the TestClass(or somewhere else), print it, this is the result :

Punto Fijo, Venezuela 
None

I don't know how to handle it or why is it caused, since it's the same function that worked fine earlier by itself. But it only gives me that result if I import the function to another file. Can I get some advice about why does it happen and how to solve it?

Eby Jacob
  • 1,418
  • 1
  • 10
  • 28
Akaidmaru
  • 13
  • 3

1 Answers1

2

your city_country function does not return any value. It just prints the result and returns None (by default).

Apply those changes and your variable should have the string value you desire:

def city_country(city_name, country_name):

    """Simple code that prints City, Country"""
    result = (city_name.title() + ", " country_name.title())
    print(result)
    return result
Ali Yılmaz
  • 1,657
  • 1
  • 11
  • 28
  • 1
    Thank you so much!. Now that you mention it, I just noticed that i have never used return, I'm going to start reading even more about it in order to improve myself. Thanks again! – Akaidmaru May 11 '18 at 23:03