0

I'm beginning to learn TDD. I have just started with unit test from python. When I try to execute:

vagrant@vagrant:~/pruebaTestPython$ python test_python_daily_software.py 

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

I have read in other links that I need to rename my own functions with test_ at the beginning. However, this is exactly what I did, but it still doesn't work.

test_python_daily_software.py file:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest
import python_daily

class TestPythonSoftware(unittest.TestCase):

    def test_should_return_python_when_number_is_3(self):
        self.assertEqual('Python', python_daily.get_string(3))

    def test_should_return_daily_when_number_is_5(self):
        self.assertEqual('Daily', python_daily.get_string(5))

    if __name__ == '__main__':
        unittest.main()

and python_daily.py file:

#!/usr/bin/python
# -*- coding: utf-8 -*-

def get_string(number):
    return 'Hello'

what is wrong?

Maverick94
  • 227
  • 4
  • 15

1 Answers1

1

If your python_daily.py Python module is:

#!/usr/bin/python
# -*- coding: utf-8 -*-


def get_string(number):
    return 'Hello'

and your test_python_daily_software.py test module is:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest

import python_daily


class TestPythonSoftware(unittest.TestCase):
    def test_should_return_python_when_number_is_3(self):
        self.assertEqual('Python', python_daily.get_string(3))

    def test_should_return_daily_when_number_is_5(self):
        self.assertEqual('Daily', python_daily.get_string(5))


if __name__ == '__main__':
    unittest.main()

You should have:

$ python test_python_daily_software.py 
FF
======================================================================
FAIL: test_should_return_daily_when_number_is_5 (__main__.TestPythonSoftware)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_python_daily_software.py", line 11, in test_should_return_daily_when_number_is_5
    self.assertEqual('Daily', python_daily.get_string(5))
AssertionError: 'Daily' != 'Hello'

======================================================================
FAIL: test_should_return_python_when_number_is_3 (__main__.TestPythonSoftware)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_python_daily_software.py", line 8, in test_should_return_python_when_number_is_3
    self.assertEqual('Python', python_daily.get_string(3))
AssertionError: 'Python' != 'Hello'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=2)

Mind your indentation in source code!

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103