集成测试:在"unittest.setUp"期间启动阻塞服务器,然后再对其进行测试? [英] Integration testing: Start a blocking server during `unittest.setUp` before testing it?

查看:93
本文介绍了集成测试:在"unittest.setUp"期间启动阻塞服务器,然后再对其进行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Thrift 编写服务,并且需要进行一些测试以确保它如预期般运作/回应.为此,最可靠的方法似乎是使用unittest模块.

I'm writing a service using Thrift and need to apply some tests to ensure that it operates/responds as expected. To accomplish this, the most robust approach seems to be to use the unittest module.

我想直接在单元测试的setUp方法中以测试"模式(从特定的测试"端口启动,使用测试"数据等)启动服务,但调用serve()此时等待连接.

I'd like to start the service in "test" mode (starts on a specific "test" port, uses "test" data, etc) from directly within the unit test's setUp method, but calling serve() blocks at that point waiting for connections.

启动服务以使测试能够执行并使用tearDown方法彻底关闭服务的最佳方法是什么?

What would be the best approach for starting the service so that the tests can execute and the service can be brought-down cleanly using the tearDown method?

推荐答案

unittest提供的完全隔离"非常适合 unit 测试(它是设计的 (但不一定要用于 integration 测试)-我看到了对那些重复使用unittest的吸引力,我自己动手利用了我们周围的特殊测试运行程序& c的优势,但是,意识到使用unittest进行集成测试非常合适,因此我尝试通过编码来进行补偿,而编码方式与我编写单元测试时完全不同.

The "complete isolation" that unittest provides is excellent for unit tests (what it's designed for) but not necessarily for integration tests -- I see the attraction of reusing unittest for those, I do it myself to take advantage of special test runners &c that we have around, but, realizing that it's something of a force fit to use unittest for integration tests, I try to compensate by coding rather differently than I'd code unit tests.

当我必须在集成测试中生成服务器时,我倾向于在模块开始时,在单独的过程中(通过subprocess或通过其他适合您的安装的方式)生成服务器以便在单独的节点或其他节点中运行它-并向 atexit注册当我的测试模块全部完成后,将向该服务器发送终止请求的终止代码.这没有 unit 测试所要求的干净地分开",但是我发现它足以进行集成测试,并分摊了启动和初始化服务器的开销,实际上跨多个服务器的开销可能确实很高.测试.

When I have to spawn a server in integration testing, I tend to do the spawning at the start of the module, in a separate process -- via subprocess, or by other means appropriate to your installation, if you want to run it in a separate node or whatever -- and register with atexit the termination code that will send that server the termination request when my test-module is all finished. That's not as "cleanly separated" as unit testing would require, but I find it's good enough for integration testing, and amortizes the overhead of starting and initializing the server, which may be very high indeed, across multiple tests.

即使您热衷于使用setUptearDown,我仍然建议您使用单独的进程(和单独的节点,如果您在连续构建场"之类的地方有很多这样的节点, ).就像@Ned的答案所暗示的那样,在同一过程中使用不同的线程会让我感到冒险-可能很容易在服务器和测试之间产生不必要的交互,从而隐藏一些错误或引起其他错误.

Even if you're keen to use setUp and tearDown, I still recommend using a separate process (and a separate node, if you have plenty of those lying around in a "continuous build farm" or the like). Using a different thread in the same process, as @Ned's answer suggests, strikes me as risky -- might easily produce unwanted interactions between the server and the tests, hiding some bug or causing others.

如果我正确理解,您不仅要尝试在同一进程上运行服务器,还要在与测试相同的线程中运行服务器,这对我来说绝对不是一个好主意-当然,它会阻止所有操作,除非服务器的编码确实非常特别!-)

If I understand correctly you're trying to run the server not just on the same process, but even in the same thread as the tests, and that seems definitely a bad idea to me -- of course it will block everything, unless the server is coded very particularly indeed!-)

这篇关于集成测试:在"unittest.setUp"期间启动阻塞服务器,然后再对其进行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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