一个nose插件,方便创建访问mongo引擎结构的自动测试。

nose-mongoengine的Python项目详细描述


Info:A nose plugin to facilitate the creation of automated tests that access Mongo Engine structures.
Repository:https://github.com/mbanton/nose-mongoengine/
PyPI page:http://pypi.python.org/pypi/nose-mongoengine/
Author:Marcelo Anton (http://github.com/mbanton) & Maxwell Dayvson ( https://github.com/dayvson/)
https://secure.travis-ci.org/mbanton/nose-mongoengine.png?branch=master

最初基于mongo nose(http://pypi.python.org/pypi/mongonose/)。感谢:kapil thangavelu

Installation

使用pip:

pip install nose-mongoengine

Configuration

插件通过几个选项扩展了nose选项。唯一 必需的选项是--mongoengine--mongoengine-mongodb-bin以启用 插件。

  • ^{tt1}$ is required to enable the plugin.
  • ^{tt2}$ Allows specifying the path to the ^{tt5}$ binary. If not specified the plugin will search the path for a mongodb binary. If one is not found, an error will be raised.
  • ^{tt6}$ Optionally clear data in db after every module of tests.
  • ^{tt7}$ Optionally clear data in db after every class of tests.
  • ^{tt8}$ can be optionally set, by default the plugin will utilize a a random open port on the machine.
  • ^{tt9}$ Enables the javascript scripting engine, off by default.
  • ^{tt10}$ Stores the server log at the given path, by default sent to /dev/null
  • ^{tt11}$ Enables pre-allocation of databases, default is off. Modern filesystems will sparsely allocate, which can speed up test execution.

该插件将创建一个mongo db实例,并创建一个空数据库来使用它。

Usage in your test cases

因为这是您使用MongoEngine的模型(model_one.py):

# encoding:utf-8 #
from mongoengine import *

class ModelOne(Document):
    int_value1 = IntField()
    int_value2 = IntField()
    boolean_value = BooleanField(required=True, default=False)

    @classmethod
    def get_model_one_by_value1(cls, v):
        return ModelOne.objects(int_value1=v)

    @classmethod
    def get_model_one_by_boolean_value(cls, v):
        return ModelOne.objects(boolean_value=v)

这是一个使用Test Nose+Nose MongoEngine(test_model_one.py)的示例:

# encoding:utf-8 #
from model_one import ModelOne
from nose.tools import assert_equals

class TestModelOne(object):

    # This method run on instance of class
    @classmethod
    def setUpClass(cls):

        global o1_id, o2_id

        # Create two objects for test
        o1 = ModelOne()
        o1.int_value1 = 500
        o1.int_value2 = 123
        o1.boolean_value = True
        o1.save()

        o2 = ModelOne()
        o2.int_value1 = 500
        o2.int_value2 = 900
        o2.boolean_value = False
        o2.save()

        # Save the id of objects to match in the test
        o1_id = o1.id
        o2_id = o2.id

    # This method run on every test
    def setUp(self):
        global o1_id, o2_id
        self.o1_id = o1_id
        self.o2_id = o2_id

    def test_match_with_value1(self):
        find = ModelOne.get_model_one_by_value1(500)
        assert_equals(len(find), 2)
        assert_equals(find[0].id, self.o1_id)
        assert_equals(find[1].id, self.o2_id)

    def test_match_with_boolean_value(self):
        find = ModelOne.get_model_one_by_boolean_value(True)
        assert_equals(len(find), 1)
        assert_equals(find[0].id, self.o1_id)

在命令行中运行:

$ nosetests --mongoengine test_model_one.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.054s

OK

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JPanel不会对键绑定做出反应   当时间大于零时,不得在UI线程上调用java Await   JTextArea的java线程安全。追加   Java用户输入的字和行计数器   java以spreedsheat格式将数据保存到文件中   java构造函数的意义是什么?   java findViewById返回null,尽管组件的ID存在   java如何向按钮添加图像   java如何中断ExecutorService的线程   java如何将属性(例如枚举)绑定到不同类型的组件属性(例如每个枚举的映像)?   随机森林分类器的java实现   html使用java连接到一个站点并发布,HTTP状态代码200   从类访问属性时发生java编译错误   Java自动填充ArrayList,搜索更好的选项