mardi 12 février 2019

Function not defined when running tests in Python package

I am creating a Python package/library. My directory structure looks like this:

my_package/
|-- my_package/
|   |-- tests/
|   |   |-- __init__.py
|   |   |-- my_tests.py
|   |   
|   |-- __init__.py
|   |-- main.py
|
|-- setup.py

I have all my functions in the main.py file:

def sum_nums(a,b):
    res = a + b
    return(res)

def mult_nums(a,b):
    res = a * b
    return(res)

def sub_nums(a,b):
    res = a - b
    return(res)

my_tests.py looks like this:

from unittest import TestCase

import my_package

def test_sum():
    assert add_numbers(3,4) == 7

def test_mult():
    assert mult_numbers(3,4) == 12

def test_sub():
    assert sub_numbers(3,4) == -1

When I run my tests from the package root directory as follows:

python setup.py test

... I get the following error:

NameError: name 'add_numbers' is not defined

  • Is my package directory structure correct?
  • Am I missing an _ init _.py file?
  • Does every directory require an _ init _.py file?
  • Is it okay to place all my functions inside a single main.py file without using if name == "main"?

Aucun commentaire:

Enregistrer un commentaire