Main Content

Pytest - starting example

Archive - Originally posted on "The Horse's Mouth" - 2016-01-07 23:12:09 - Graham Ellis

"pytest is a mature full-featured Python testing tool that helps you write better programs" ... and being fully featured, there's something of a hurdle for newcomers in finding which falities they need from the wide range. To help a newcomer today, we took a short look at some first Pytest examples .. and I've now loaded those onto our website for your (and my later) reference.

Firstly - "Hello testing world" defines a function to be tested:

  def add(this,that):
    result = this + that
    return result


and a test for that function:

  def test_add():
    assert add(10,15) == 25


and when I run that, the test is discovered and run - and it passes:

  WomanWithCat:pt grahamellis$ python -m pytest test_hello.py
  ========================== test session starts ==========================
  platform darwin -- Python 2.7.5, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
  rootdir: /Users/grahamellis/jan16/cyan/pt, inifile: setup.cfg
  collected 1 items
  
  test_hello.py .
  
  ======================= 1 passed in 0.00 seconds ========================
  WomanWithCat:pt grahamellis$


At this early stage, before we have added a configuration file, any methods starting test_ will be run as part of the test suite. The complete example is available [here]