How to organize and run unittests and functional tests separately using nosetests

不打扰是莪最后的温柔 提交于 2019-12-12 10:36:30

问题


I have the following typical python project file structure

packageA
   +----subpackage1
            +----classa.py
   +----subpackage2
            +----classb.py
   +----test
          +----subpackage1
                    +----classa_test.py 
          +----subpackage2
                    +----classb_test.py

I am currently trying to organize my unittests and functional tests so I can run unittests and functional tests separately using nose but also have the option to run all tests. The tests would live in packageA/test/subpackage1 and packageA/test/subpackage2.

  • What is a good way to organize the different tests? By folder (functional/ vs unit/) ? By naming convention of test class (ClassATest vs ClassAFunctionalTest)? or by naming convention of test methods (classa_foo_test vs classa_bar_functional_test)?
  • Can someone explain how nosetests's regex matching works? The options -m, -i and -e don't seem to run as I expect to run. Does the regex match directories (subpackage1), files (classa_test) or test classes (ClassATest) or test methods (classa_foo_test)? I am extremely confused

回答1:


My tests directory structure looks this way:

root
  + --- tests
  |       + --- unit_tests
  |       |         + --- some_app_tests   
  |       |         + --- another_app_tests
  |       |         | run_tests.py
  |       |
  |       + --- integrate_tests 
  |                 + --- some_app_tests
  |                 + --- another_app_tests
  |                 | run_tests.py
  |       
  + --- project_root
          + --- some_app
          + --- another_app

For each individual app I create coresponding directory with tests in unit- and integrate- directory. Each is directory is separate django project with custom settings and there's management command used to run tests.

Also placing tests in one directory has one nice advantage - when project is deployed, there's no reason to deploy tests with it. So I just strip one directory and that's all.

(to run tests I use django-sane-testing: https://github.com/Almad/django-sane-testing )




回答2:


If you are developing Django project, you can try this library: unclebob https://github.com/gabrielfalcao/unclebob

It suggest a way how to organize and run your unit tests and functional tests.




回答3:


I would try to organize the test by functional area. I don't really know what nose is.

But if you for example testing a login area for a webpage then create a subfolder called "login" or "loginTests", and for menu test create a "menu" or "menuTests" folder. It is always good to have good naming conventions as well, so name the test and folders exactly what they are testing. Be as specific as you can be.



来源:https://stackoverflow.com/questions/8043417/how-to-organize-and-run-unittests-and-functional-tests-separately-using-nosetest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!