def main(): program = ["def sayHello():", "\n\tprint('Hello World!')"] f = open('hello.py', 'w+') for line in program: f.write(line) f.close() import hello hello.sayHello()
def test_sayHello_name(self): result = hello.sayHello("Alex") self.assertEqual(result, "Hello, Alex")
def test_hello(self): self.assertEqual(hello.sayHello(), "Hello World!!")
import hello print hello.sayHello() print hello.sayName("Martyn")
def test_say_hello_with_input(): # Override the Python built-in input method hello.input = lambda x: 'Python' output = hello.sayHello() assert 'Hello Python, nice to meet you!' == output
def test_say_hello_without_input_with_mock(): with mock.patch.object(hello, 'input', lambda x: ''): output = sayHello() assert 'Hello World' == output
def test_say_hello_with_input_with_mock(): with mock.patch.object(hello, 'input', lambda x: 'Python'): output = sayHello() assert 'Hello Python, nice to meet you!' == output
def test_sayHello_to_nobody(self): with self.assertRaises(Exception): hello.sayHello("")
def test_SayHello(self): # If the method returns "Hello World!", then the test will pass self.assertEqual(hello.sayHello(),"Hello World!")
def test_sayHello_without_title(self): result = hello.sayHello("Senko") self.assertEqual(result, "Hello Mr. Senko")
def test_sayHello_with_title(self): result = hello.sayHello("Senko", title="The Misterious") self.assertEqual(result, "Hello The Misterious Senko")
import hello name = input("What is your name? ").title() print("You have terminal cancer") hello.sayHello(name) print("You Gay") hello.sayBye(name)
def test_sayHello_to_one_person(self): result = hello.sayHello("Alex") self.assertEqual(result, "Hello, Alex")
def test_sayHello_to_many_people(self): with self.assertRaises(Exception): hello.sayHello("Alex,Krasi")
def test_sayHello_name_with_greeting(self): result = hello.sayHello("Alex", "Happy testing") self.assertEqual(result, "Happy testing, Alex")
def test_say_hello_without_input(): # Override the Python built-in input method hello.input = lambda x: '' output = hello.sayHello() assert 'Hello World' == output
# test.py import hello hello.sayHello()
import hello hello.sayHello() hello.sayHello('zy')