我正在为一个Python脚本构建一个测试程序,该脚本将与rejectdb数据库一起执行工作。作为setUp()方法的一部分,我试图让测试人员在端口28016上的localhost上启动RejectDB服务器,以防尚未启动。在
我用subprocess启动服务器。问题是,根据https://docs.python.org/2/library/subprocess.html,subprocess等待命令完成。在这种情况下,似乎只要服务器启动并运行,过程就不完整,测试也不会继续超过setUp()阶段。在
下面是我尝试的脚本:import unittest
import rethinkdb as r
import subprocess
class TestController(unittest.TestCase):
HOST = "localhost"
PORT_OFFSET = 1
PORT = 28015 + PORT_OFFSET
DB = "ipercron"
TABLE = "sensor_data"
def setUp(self):
try:
self.conn = r.connect(self.HOST, self.PORT)
except r.ReqlDriverError:
print("The RethinkDB server is not yet ready. Starting it up...")
subprocess.call(["rethinkdb", "--port-offset", str(TestController.PORT_OFFSET)])
self.conn = r.connect(self.HOST, self.PORT)
if TestController.DB not in r.db_list().run(self.conn):
r.db_create(TestController.DB).run(self.conn)
self.conn.use(TestController.DB)
if TestController.TABLE not in r.table_list().run(self.conn):
r.table_create(TestController.TABLE).run(self.conn) # Create the table if it does not yet exist
r.table(TestController.TABLE).delete().run(self.conn) # Empty the table to start with a clean slate
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
suite = unittest.TestLoader().loadTestsFromTestCase(TestController)
unittest.TextTestRunner(verbosity=2).run(suite)
subprocess用于在命令行执行rethinkdb --port-offset 1命令,然后继续执行脚本。但是,当我运行脚本时,会收到一条通常的消息:服务器已就绪:
^{pr2}$
没有任何进一步的行动。如何让unittest继续进行测试?在