Question: Hi, I need help writing tests for this code below which is about a program that sends a random inspirational quote to an email address
Hi, I need help writing tests for this code below which is about a program that sends a random inspirational quote to an email address using python
- there should be 3 different unit tests that run using mocks and spies: 1 -the function that actually sends emails gets called 2- it gets called once, not twice 3 -it is called correctly, with the correct arguments
- This is a project that sends emails, it is about demonstrating understanding of mocks/spies
- more info about the testing on the images at the end
- change gimail into its correct spelling, chegg doent allow me to post with the correct spelling
# main.py # Import section .... import os import smtplib import datetime as dt import random import sys from dotenv import load_dotenv # Loading environmental variables load_dotenv()
# Constants section .... # EMAIL account sender SENDER = sys.argv[1] # SENDER = os.getenv('SENDER') PASSWORD = os.getenv('PASSWORD') # Accounts receivers RECEIVER = os.getenv('RECEIVERS') # SMTP server options HOST = os.getenv('HOST') PORT = os.getenv('PORT') def email_random(): now = dt.datetime.now() weekday = now.weekday() if weekday == 4: with open("quotes.txt") as quote_file: all_quotes = quote_file.readlines() quote = random.choice(all_quotes) with smtplib.SMTP("smtp.gimail.com") as connection: connection.starttls() connection.login(SENDER, PASSWORD) connection.sendmail( from_addr=SENDER, to_addrs=RECEIVER, msg=f"Subject:Friday Motivation {quote}" ) return quote email_random()# .env environment variables that we need to configure our SMTP client cat .env #!/bin/sh
export SMTP_SERVER=smtp.gimail.com export SMTP_PORT=587 export SMTP_LOGIN= export SMTP_PASSWORD=
# quote.txt file with quotes
cat quotes.txt
"When you arise in the morning think of what a privilege it is to be alive, to think, to enjoy, to love..." - Marcus Aurelius "Either you run the day or the day runs you." - Jim Rohn "Mondays are the start of the work week which offer new beginnings 52 times a year!" - David Dweck "You've got to get up every morning with determination if you're going to go to bed with satisfaction." - George Lorimer "Be miserable. Or motivate yourself. Whatever has to be done, it's always your choice." - Wayne Dyer "Your Monday morning thoughts set the tone for your whole week. See yourself getting stronger, and living a fulfilling, happier & healthier life." - Germany Kent "You don't have to be great to start, but you have to start to be great." - Zig Ziglar



Write a program that sends a random inspirational quote to an email address. The email address should be a command-line parameter passed to the program. You should have a list of quotes in a file by itself. Your program should grab one and send the email. Your final email quotes should be formatted like this: "The only true measure of success if the number of people you have helped" Ray Dalio Node After npm init you should add your own script to package.json. Look for scripts inside the file. you should be able to do the following: npm run send_inspiration ... The command needs to allow you to pass in an email address from the command line. Take a look at this Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the right functions will need to be executed. Python You should be able to run your code using python send_inspiration.py ... The command needs to allow you to pass in an email address from the command line. Take a look at this Go with the simplest option you can find initially - because KISS Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the Also note that good code is written to be reusable. Make sure all your executable code is inside useful functions. When we run npm run send_inspiration then the right functions will need to be executed. Testing your project Your unit tests should make sure that when the application runs then it sends one email with one quote to one person. If your tests send actual emails require your smtp secret values then they are wrong. Please make sure that you understand what mocks and spies are for when writing your tests. These things do not exist to take up space, waste time or look fancy. They do have a purpose. If you do anything that looks like the following pseudocode then you are doing it wrong: spyon (myFunction) myFunction() assert myFunction wasCalleonce Here is a useful way to think about mocks and spies: Basically, sending emails is considered "expensive". Why? Because it costs actual money if you do it in bulk. Also it costs time. It is not a fast and free operation. But it is an important operation. You need to test that when you want to send an email then: 1. the function that actually sends emails gets called 2. it gets called once, not twice 3. it is called correctly, with the correct arguments You don't want your unit tests to actually send emails. You just want them to prove that the part of your machine that sends emails is being used correctly. Instructions for reviewer Unit tests: The learner should demonstrate that the email sends only once, with the correct arguments. The learner should demonstrate an understanding of mocks and spies. Please see TOPICS: Unit testing with mocks and spies. Sending emails to multiple recipients is not a requirement, but if the code is structured well enough, then doing so should be easy. The unit test should demonstrate that the code in not restricted to sending just one email
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
