python test code example

Example 1: format for unit test python

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

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

Example 2: python testing

>>> assert sum([1, 2, 3]) == 6, "Should be 6"

Example 3: python quiz

score = 0
score = int(score)

#Ask user for their name
name = input("What is your name?")
name = name.title()
print("""Hello {}, welcome to Quiz night! 
You will be presented with 5 questions.
Enter the appropriate number to answer the question
Good luck!""".format(name))

#Question1
print("""Question here
1.  
2.  
3.  
4. """)

answer1 = "correct number here"
response1 = input("Your answer is: ")

if (response1 != answer1):
    print("Sorry, that is incorrect!")
else:
    print("Well done! " + response1 + " is correct!")
    score = score + 1

print("Your current score is " + str(score) + " out of 5")

#Question2
print("""What is the name of the town your character lives in before The Great War?
1. Diamond City 
2. Sanctuary Hills 
3. Concord 
4. The Glowing Sea""")

answer2 = "correct number here"
response2 = input("Your answer is:")

if (response2 != answer2):
    print("Sorry, that is incorrect!")
else:
    print("Well done! " + response2 + " is correct!")
    score = score + 1

print("Your current score is " + str(score) + " out of 5")

#Question3
print("""Question here
1.  
2.  
3.  
4. """)

answer3 = "correct number here"
response3 = input("Your answer is:")

if (response3 != answer3):
    print("Sorry, that is incorrect!")
else:
    print("Well done! " + response3 + " is correct!")
    score = score + 1

print("Your current score is " + str(score) + " out of 5")

#Question4
print("""Question here
1. 
2. 
3. 
4. """)

answer4 = "correct number here"
response4 = input("Your answer is:")

if (response4 != answer4):
    print("Sorry, that is incorrect!")
else:
    print("Well done! " + response4 + " is correct!")
    score = score + 1

print("Your current score is " + str(score) + " out of 5")

#Question5
print("""Question here
1. False
2. True""")

answer5 = "correct number here"
response5 = input("Your answer is:")

if (response5 != answer5):
    print("Sorry, that is incorrect!")
else:
    print("Well done! " + response5 + " is correct!")
    score = score + 1

print("Your total score is " + str(score) + " out of 5")
print("Thank you for playing {}, goodbye!".format(name))

Example 4: self.assertequal python

'''
In other words, assertEquals is a function to check if two variables are equal, for purposes of automated testing:
'''

def assertEquals(var1, var2):
    if var1 == var2:
        return True
    else:
        return False

Example 5: python online test

YOU CAN RUN YOUR PYTHON CODE IN W3SCHOOLS PYTHON