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

发布于 2024-12-14 05:03:57 字数 746 浏览 0 评论 0原文

我有以下典型的Python项目文件结构,

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

我目前正在尝试组织我的单元测试和功能测试,这样我就可以使用nose单独运行单元测试和功能测试,但也可以选择运行所有测试。测试将位于 packageA/test/subpackage1 和 packageA/test/subpackage2 中。

  • 组织不同测试的好方法是什么?按文件夹(功能/与单元/)?通过测试类的命名约定(ClassATest vs ClassAFunctionalTest)?或者通过测试方法的命名约定(classa_foo_test vs classa_bar_function_test)?
  • 有人可以解释一下nosetests的正则表达式匹配是如何工作的吗?选项 -m、-i 和 -e 似乎没有按我预期的方式运行。正则表达式是否匹配目录(subpackage1)、文件(classa_test)或测试类(ClassATest)或测试方法(classa_foo_test)?我非常困惑

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拍不死你 2024-12-21 05:03:58

我的测试目录结构如下所示:

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

对于每个单独的应用程序,我创建相应的目录,并在单元和集成目录中包含测试。每个目录都是具有自定义设置的独立 django 项目,并且有用于运行测试的管理命令。

此外,将测试放在一个目录中还有一个很好的优点——部署项目时,没有理由用它来部署测试。所以我只删除一个目录就这样了。

(要运行测试,我使用 django-sane-testing: https://github.com/Almad/django -sane-测试

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 )

尝蛊 2024-12-21 05:03:58

如果你正在开发 Django 项目,你可以尝试这个库:unclebob https://github.com/gabrielfalcao/unclebob

它提出了一种如何组织和运行单元测试和功能测试的方法。

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.

无悔心 2024-12-21 05:03:58

我会尝试按功能区域组织测试。我实在不知道鼻子是什么。

但是,如果您例如测试网页的登录区域,则创建一个名为“login”或“loginTests”的子文件夹,并为菜单测试创建一个“menu”或“menuTests”文件夹。拥有良好的命名约定总是好的,因此将测试和文件夹命名为它们正在测试的内容。尽可能具体。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文