Question

Using Python Unit Test, How can I ensure that a particular string is printed , for...

Using Python Unit Test, How can I ensure that a particular string is printed , for example, how can I ensure that "hello world" is printed and If the string is different for example "Hello Chegg" it will raise an error?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You can use self.assertEqual() to check whether the string is 'hello world' or not and if it is not 'hello world' then the string won't be printed and will show Failed test.

Code for passed testcase along with image of the code and output:

import unittest

class stringTest(unittest.TestCase):
  
def test_helloWorld(self):
self.assertEqual('hello world', 'hello world') # Checking whether the strings are equal or not
print('hello world')


if __name__ == '__main__': # Main Function to enter in the program
unittest.main()

Code for failed testcase along with image of the code and output:

import unittest

class stringTest(unittest.TestCase):
  
def test_helloWorld(self):
self.assertEqual('hello world', 'Hello Chegg') # Checking whether the strings are equal or not
print('hello world')


if __name__ == '__main__': # Main Function to enter in the program
unittest.main()

Explanation: When both the string in self.assertEqual() are same then the unit test will be passed and the statement below the unit test line will be printed. While when both the strings are different the statement below the unit test line won't be printed.

If the answer helped please upvote. It means a lot. For any query comment below.

Add a comment
Know the answer?
Add Answer to:
Using Python Unit Test, How can I ensure that a particular string is printed , for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON PLEASE 1. Let the user input a string. (10 points) 2. Display how many...

    IN PYTHON PLEASE 1. Let the user input a string. (10 points) 2. Display how many words the user inputs. (30 points) 3. display number of words is either odd or even. (40 points) For example: • Example 1: Please input a string: Hello World Number of words: 2 It is an even number • Example 2: Please input a string: Python Programming for Non Major Number of words: 5 It is an odd number

  • python how can I print "Hello 55!" how can i add the exclamation mark at the...

    python how can I print "Hello 55!" how can i add the exclamation mark at the end using a comma to add my variable? name = 55 print( "Hello", name )  

  • On Python 3.7.2: How would I make functions for these string functions/methods without using the actual...

    On Python 3.7.2: How would I make functions for these string functions/methods without using the actual functions? (For example, how would I find if a string is comprised of alphabets without using the function "isalpha()" ?) len() isalpha() isupper() isdigit() swapcase() string_lower()

  • Python Write a program which reads in a string and an signifying the number times the...

    Python Write a program which reads in a string and an signifying the number times the string should be repeated. The program should then output the string N times. integer N, You can assume all input will be valid. Example: Enter a string: hello How many times to repeat? 5 hello hello hello hello hello Example: T Enter a string: goodbye Ty How many times to repeat? 32 goodbye goodbye [... goodbye 30 more times]

  • Hi so I want to run a Python script (in this example it can be something...

    Hi so I want to run a Python script (in this example it can be something as simple as print hello world) when a button on a Python Flask Server is pressed. In other words when I enter the web server, there will be a button and pressing it will launch the python script on my computer. Thank you

  • In Python: LoadFile is a function that takes in a string (a filename) and then returns...

    In Python: LoadFile is a function that takes in a string (a filename) and then returns a list. The list is the contents of the file, where each element is a list of data from the file. Here's an example of using this function. The input file had four lines of text. >>> lines = LoadFile("test.txt") >>> print("OUTPUT", lines) OUTPUT ["Hello there", "I am a test file", "please load me in and print me out", "Thanks"]

  • Part I. Representation of a String The primitive type char is used to store a single...

    Part I. Representation of a String The primitive type char is used to store a single character, whether it is a letter, number or special character (for example: space, tab, or new line). More often, we want to store a string of characters in a single variable. We can do this by using a variable of the class type String. A String variable can be initialized to a String literal (which is similar to how a char variable can be...

  • IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can...

    IN PYTHON 3) Number of Words Write a function numWords() which takes in a string can prints the number of words in the string. You can assume that words will only be separated with spaces, commas, and periods. "hello, world" -> 2 "this is cool" -> 3 4) Is Sorted Write a function isSorted() which takes in an list of integers and returns true if the numbers are sorted from smallest to largest.

  • Java 1. Can you explain it how it will be printed step by step? Thanks!! What...

    Java 1. Can you explain it how it will be printed step by step? Thanks!! What is printed by the following? for (int i=1; i<4; i++) { for (char c=’a’; c<=’c’; c++) { if (i%2==0) { i++; System.out.println(i + " " + c); } else { c++; System.out.println(c + " " + i); } } } 2. Is there a compiler error in this code? If so, what is it? If not, what’s printed? Is there a compiler error in...

  • Using Python: #Write a letterAppearance function that takes a string, str as argument #letterAppearance will count...

    Using Python: #Write a letterAppearance function that takes a string, str as argument #letterAppearance will count the number of times each letter appears in a string #using a dictionary. #letterAppearance returns only the letters that appear in the string. #Capitalization should not matter ''' letterAppearance('hello') #{'h': 1, 'e': 1, 'l': 2, 'o': 1} letterAppearance('Hello') #{'h': 1, 'e': 1, 'l': 2, 'o': 1} letterAppearance('mutation') #{'m': 1, 'u': 1, 't': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1} '''

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT