Coder Social home page Coder Social logo

gyhou / lambda-lecture-pytest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from prof-rossetti/lambda-lecture-pytest

0.0 1.0 0.0 15 KB

Exercises in automated testing in Python, for students at the Lambda School!

License: Other

Python 100.00%

lambda-lecture-pytest's Introduction

Lambda Lecture (Pytest)

Exercises in automated testing in Python, for students at the Lambda School!

For an introduction to automated testing and software quality control at a conceptual level, feel free to consult these notes and these slides.

Setup

Fork this repo under your own control. Then download or clone it onto your local machine, and navigate into its root directory from the command-line:

cd lambda-lecture-pytest/

Create and activate a virtual environment (if you like that kind of thing):

conda create -n game-env python=3.7 # first time only
conda activate game-env

From within the virtual environment (if you're using one), install the "pytest" package:

pip install pytest

OK, after installing "pytest" you're ready to proceed with the exercises!

Game Exercise

For this first exercise, we'll be playing a simple game!

In the file called "app/game.py" there is an unfinished game of rock-paper-scissors. Play it:

python app/game.py

After playing the game one or more times, you'll realize it erroneously always results in a tie. Hey, that's not right! Your objective is to fix the bug in the game's logic so it properly determines the real winner.

Before you move on to tackling this objective, take a minute to observe the contents of the test file called "test/game_test.py", and run it:

pytest test/game_test.py

You'll see an "AssertionError", which means the test is failing. Your second objective is to make the tests pass, and expand the number of scenarios tested.

Instructions:

  1. Uncomment the "TODO" on line 42, and replace it with: winning_choice = determine_winner(user_choice, computer_choice).
  2. Incrementally update the logic inside the determine_winner() function, and manually test the game after each iteration by playing it to ensure it is working properly.
  3. As you are iteratively developing, notice what manual testing actions you are taking and find opportunities to automate those activities by iteratively running the game test file and revising / expanding its contents as necessary.

Parser Exercise

For this exercise, we're going to issue an HTTP request to a stock market data API, and attempt to determine what the latest closing price is for the given stock symbol. To issue the requests, we'll need a few packages, so install them now:

pip install requests python-dotenv

Run the parser script to see it issue the request and print the response data and the latest closing price:

python app/parser.py

After running the script one or more times, you'll realize it erroneously calculates the closing price. Hey, that's not right! Your objective is to fix the bug in the game's logic so it can properly calculate the latest closing price.

Before you move on to tackling this objective, take a minute to observe the contents of the test file called "test/parser_test.py", and run it:

pytest test/parser_test.py

You'll see one test is passing, but the other is failing. NOTE: There is no need to update the contents of the parser test file, because when you are finished tackling your objective, all tests should pass.

Instructions:

  1. Incrementally update the logic inside the latest_closing_price() function and re-run the parser test file until all its tests pass.
  2. After the tests are passing, try running the parser script again, and observe it should work with live data as well. This is because the mock data structure used in the test resembles the real data structure returned by the API.
  3. Observe that you have successfully developed the logic of the latest_closing_price() in a test-driven manner, and without issuing unnecessary HTTP requests!

Further Exploration

Thanks for your attention and I hope you enjoyed these exercises. ๐ŸŽ‰ ๐Ÿ™Œ If you'd like to contribute an update or a fix to this repo, PRs are welcome!

For more test-driven data processing exercises, see the Omniparser Exercise.

lambda-lecture-pytest's People

Contributors

s2t2 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.