在多个文件中进行测试 [英] Having tests in multiple files

查看:52
本文介绍了在多个文件中进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大型项目使用金字塔框架,但我发现将所有测试放在一个 tests.py 文件中很麻烦.所以我决定创建包含我的测试文件的目录.问题是,我不知道如何告诉金字塔从这个目录运行我的测试.

I am using pyramids framework for large project and I find it messy to have all my tests in one tests.py file. So I have decided to create directory that would contain files with my tests. Problem is, I have no idea, how to tell pyramids, to run my tests from this directory.

我正在使用 python setup.py test -q 运行测试.

I am running the tests using python setup.py test -q.

但这当然不起作用,在我将我的测试移动到测试目录之后.该怎么做才能让它发挥作用?

But this of course do not work, after I have moved my tests into tests directory. What to do, to make it work?

推荐答案

首先,您需要通过创建 __init__ 来确保 tests 不仅仅是一个目录,而是一个 Python 包.py 在里面.

First, you need to make sure tests is not just a directory, but a Python package by creating an __init__.py in it.

您还需要确保在 teststest_something.py 中为模块命名.

You also need to make sure you name the modules in your tests package test_something.py.

大多数测试运行者,作为他们测试发现的一部分,寻找名为 tests 的模块或包,该包中的模块以 test_ 开头,并期待测试的方法名称方法(在 TestCase 子类上)以 test_ 开头.

Most test runners, as part of their test discovery, look for a module or package named tests, modules in that package starting with test_ and expect method names for test methods (on TestCase subclasses) to start with test_.

unittest 模块将测试运行器描述为:

The unittest module describes test runners as:

测试运行器是协调测试执行的组件并将结果提供给用户.跑步者可以使用图形interface,一个文本界面,或者返回一个特殊值来表示执行测试的结果.

A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

有很多不同的测试框架和测试运行器,大多数以某种方式扩展了 unittest 并寻找 unittest.TestCase 子类.他们可能会进行不同类型的测试发现,将结果呈现在不同的方式或在运行测试时收集代码覆盖率.

There are plenty of different testing frameworks and hence test runners out there, most extending unittest in some way and looking for unittest.TestCase subclasses. They may do different types of test discovery, present the results in a different way or gather code coverage while the tests are being run.

至于相对导入:你真的应该尽量避免这些.它们使移动代码变得更加困难(正如您刚刚注意到的)并降低了导入的可读性(从哪里导入代码?).只需使用 from myproject.views import my_view - 它的位置更清晰

As for relative imports: You should really try to avoid these. They make it harder to move code around (as you just noticed) and decrease the readability of the imports (where does what code get imported from?). Just use from myproject.views import my_view - it's a lot clearer where things live

这篇关于在多个文件中进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆