2011-10-07 10 views
19

Purtroppo ho notato che ci sono troppi modi per mantenere l'unittest in Python e di solito non sono ben documentati.Organizzazione ottimale della struttura dei file di un modulo Python?

Sto cercando una struttura "finale", si potrebbe realizzare la maggior parte dei requisiti di seguito:

  • essere rilevabile da framework di test, tra cui:
    • pytest
    • nosetests
    • tox
  • i test dovrebbero essere outside the module files e in un'altra directory rispetto al modulo stesso (manutenzione), probabilmente in una directory tests/ a livello di pacchetto.
  • dovrebbe essere possibile eseguire solo un file di test (il test deve essere in grado di sapere dove è il modulo che si suppone per testare)

Si prega di fornire un file di test di esempio che fa un test di falso, specificare nomefile e directory.

+0

Qual è la tua domanda, davvero? Perché non usi solo uno dei quadri e lasci che tutti facciano come vogliono? – pvoosten

risposta

17

Ecco il metodo che ho usato:

struttura Directory

# All __init__.py files are empty in this example. 
app 
    package_a 
     __init__.py 
     module_a.py 
    package_b 
     __init__.py 
     module_b.py 
    test 
     __init__.py 
     test_app.py 
    __init__.py 
main.py 

main.py

# This is the application's front-end. 
# 
# The import will succeed if Python can find the `app` package, which 
# will occur if the parent directory of app/ is in sys.path, either 
# because the user is running the script from within that parect directory 
# or because the user has included the parent directory in the PYTHONPATH 
# environment variable. 

from app.package_a.module_a import aaa 
print aaa(123, 456) 

module_a.py

# We can import a sibling module like this. 
from app.package_b.module_b import bbb 
def aaa(s, t): 
    return '{0} {1}'.format(s, bbb(t)) 

# We can also run module_a.py directly, using Python's -m option, which 
# allows you to run a module like a script. 
# 
# python -m app.package_a.module_a 
if __name__ == '__main__': 
    print aaa(111, 222) 
    print bbb(333) 

module_b.py

def bbb(s): 
    return s + 1 

test_app.py

import unittest 

# From the point of view of testing code, our working modules 
# are siblings. Imports work accordingly, as seen in module_a. 
from app.package_a.module_a import aaa 
from app.package_a.module_a import bbb 

class TestApp(unittest.TestCase): 

    def test_aaa(self): 
     self.assertEqual(aaa(77, 88), '77 89') 

    def test_bbb(self): 
     self.assertEqual(bbb(99), 100) 

# Simiarly, we can run our test modules directly as scripts using the -m option, 
# or using nose. 
# 
# python -m app.test.test_app 
# nosetests app/test/test_app.py 

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

Grazie, per l'esempio, ma sono sicuro che se eseguirai test_app.py si lamenterà che non è in grado di trovare "app". Pensa che il test debba passare prima che il pacchetto venga distribuito al percorso di inclusione di Python. – sorin

+0

@sorin Vero, ma puoi avvicinarti molto ai due esempi forniti alla fine della mia risposta. – FMc

+0

L'intero punto era chiamare i framework di test senza parametri. – sorin

Problemi correlati