GithubHelp home page GithubHelp logo

itp-w2-hangman's Introduction

[itp-w2] Hangman

Today we are going to re-create the game 'Hangman' in Python!

It is broken down into several small functions that, when combined and completed, form a working game!

Use docstrings (comments below function definitions) and unit tests to guide your coding. To do so, we'll make use of more advanced features of Py.test.

make test

Up to this point, you've just used make test to run your tests. If you read the Makefile you'll see that make test is just an alias for PYTHONPATH=. py.test -s tests. Breaking it down:

  • PYTHONPATH=.: sets the PYTHONPATH in the current directory. Don't worry much about it. We talk about it in detail in the Advanced Python course ๐Ÿ˜‰
  • py.test: The Py.test command
  • -s tests: It's just indicating Py.test to run all the tests in the tests directory.

Try running this by yourself in the command line and see that it does the same thing as make test: PYTHONPATH=. py.test -s tests

Running a single test

Now we want to start looking at the functions in main.py and implement them. But if we run all the tests, we get a huge stacktrace that's not useful at all. We want to tackle functions one by one, and to do so, we want to run tests one by one. For example, we want to work on the _get_random_word function. To make sure it's correct we want to use the test test_get_random_word. How can we run just that one particular test? Simple, just use the -k argument of Py.test. -k takes a string as input and runs all the tests that match that string. We could do for example:

$ PYTHONPATH=. py.test -s tests -k test_get_random_word

That'll run just the test test_get_random_word. Please note that we're still using the base part of the command: PYTHONPATH=. py.test -s tests.

Tidy the results

When we just start our project we run our tests and it's just one big pile of red messages that make no sense. For example, this is how running tests with this empty project looks like:

image

There's no useful information aside than seeing that 20 tests failed. That's because we're inlcuding the "stacktrace" for all our failing tests. The stacktrace is useful when we're debugging a particular error (something not working ok), but in this case it's just noise. The way to get rid of it is by using the --tb option (stands from "traceback"). We could do for example --tb=no. This is how it looks like:

image

If you combine it with the -v option, you get:

image

Try running different options for --tb like --tb=line or --tb=short and combine them with -v.

itp-w2-hangman's People

Contributors

santiagobasulto avatar martinzugnoni avatar

Watchers

James Cloos avatar  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.