简体   繁体   English

如何使用nosetests分别组织和运行unittests和功能测试

[英]How to organize and run unittests and functional tests separately using nosetests

I have the following typical python project file structure 我有以下典型的python项目文件结构

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. 我目前正在尝试组织我的单元测试和功能测试,因此我可以使用nose单独运行unittests和功能测试,但也可以选择运行所有测试。 The tests would live in packageA/test/subpackage1 and packageA/test/subpackage2. 测试将存在于packageA / test / subpackage1和packageA / test / subpackage2中。

  • What is a good way to organize the different tests? 组织不同测试的好方法是什么? By folder (functional/ vs unit/) ? 按文件夹(功能/ vs单位/)? By naming convention of test class (ClassATest vs ClassAFunctionalTest)? 通过命名测试类的约定(ClassATest vs ClassAFunctionalTest)? or by naming convention of test methods (classa_foo_test vs classa_bar_functional_test)? 或者通过命名测试方法的约定(classa_foo_test vs classa_bar_functional_test)?
  • Can someone explain how nosetests's regex matching works? 有人能解释一下nosetests的正则表达式匹配是如何工作的吗? The options -m, -i and -e don't seem to run as I expect to run. 选项-m,-i和-e似乎没有像我期望的那样运行。 Does the regex match directories (subpackage1), files (classa_test) or test classes (ClassATest) or test methods (classa_foo_test)? 正则表达式是匹配目录(subpackage1),文件(classa_test)还是测试类(ClassATest)还是测试方法(classa_foo_test)? I am extremely confused 我非常困惑

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. 对于每个单独的应用程序,我使用unit-和integrate-目录中的测试创建相应的目录。 Each is directory is separate django project with custom settings and there's management command used to run tests. 每个目录都是单独的django项目,带有自定义设置,还有用于运行测试的管理命令。

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 ) (运行测试我使用django-sane-testing: https//github.com/Almad/django-sane-testing

If you are developing Django project, you can try this library: unclebob https://github.com/gabrielfalcao/unclebob 如果你正在开发Django项目,你可以尝试这个库:unclebob https://github.com/gabrielfalcao/unclebob

It suggest a way how to organize and run your unit tests and functional tests. 它提出了一种如何组织和运行单元测试和功能测试的方法。

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. 但是,如果您测试网页的登录区域,则创建一个名为“login”或“loginTests”的子文件夹,并为菜单测试创建一个“menu”或“menuTests”文件夹。 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. 尽可能具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM