使用鼻子/夹具/webtest 测试cherrypy(酰胺基亚硝酸盐)

发布于 2024-10-14 19:45:08 字数 771 浏览 8 评论 0原文

我正在开发 CherryPy 应用程序,我想为其编写一些自动化测试。我选择使用鼻子测试。该应用程序使用 sqlalchemy 作为数据库后端,因此我需要使用固定包来提供固定数据集。我也想做网络测试。以下是我将其全部设置在一起的方法:

在创建所有模型的文件中,我有一个辅助函数 init_model(test = False) 。它连接到生产或测试(如果 test == True 或cherrypy.request.app.test == True)数据库并调用 create_all

的测试创建了一个基类:

class BaseTest(DataTestCase):
def __init__(self):
    init_model(True)
    application.test = True
    self.app = TestApp(application)
    self.fixture = SQLAlchemyFixture(env = models, engine = meta.engine, style = NamedDataStyle())
    self.datasets = (
        # all the datasets go here
        )

然后我为这样 我通过创建 BaseTest 的子类并调用 self.app.some_method() 来进行测试,

这是我第一次在 python 中进行测试,这一切看起来非常复杂。我想知道我是否按照作者的意图使用提到的软件包,以及它是否过于复杂。

I am developing a CherryPy application and I want to write some automated tests for it. I chose to use nosetests for it. The application uses sqlalchemy as db backend so I need to use fixture package to provide fixed datasets. Also I want to do webtests. Here is how I set it all together:

I have a helper function init_model(test = False) in the file where all models are created. It connects to the production or test (if test == True or cherrypy.request.app.test == True) database and calls create_all

Then I have created a base class for tests like this:

class BaseTest(DataTestCase):
def __init__(self):
    init_model(True)
    application.test = True
    self.app = TestApp(application)
    self.fixture = SQLAlchemyFixture(env = models, engine = meta.engine, style = NamedDataStyle())
    self.datasets = (
        # all the datasets go here
        )

And now I do my tests by creating child classes of BaseTest and calling self.app.some_method()

This is my first time doing tests in python and all this seems very complicated. I want to know if I am using the mentioned packages as their authors intended and if it's not overcomplicated.

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

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

发布评论

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

评论(1

神经大条 2024-10-21 19:45:08

这看起来与任何规模的系统的普通测试胶水很相似。换句话说,它并不太复杂。

事实上,我建议在一方面稍微复杂一些:我认为您会发现在每个子测试类中设置一个新数据库非常慢。更常见的是,每次运行至少设置一次所有表,而不是每节课设置一次。然后,您可以让每个测试方法为其自身创建所需的所有数据,和/或在事务中运行每个测试用例并将其全部回滚到 finally: 块中。

That looks mostly like normal testing glue for a system of any size. In other words, it's not overly-complicated.

In fact, I'd suggest slightly more complexity in one respect: I think you're going to find setting up a new database in each child test class to be really slow. It's more common to at least set up all your tables once per run instead of once per class. Then, you either have each test method create all the data it needs for its own sake, and/or you run each test case in a transaction and roll it all back in a finally: block.

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