一个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接口中的每个方法都是抽象的,但在抽象类中,我们也只能使用抽象方法   初始化Java中声明的、未初始化的变量会发生什么情况?   java BouncyCastle openPGP将字节[]数组加密为csv文件   在Java中将类A(和所有子类)映射到类B的实例的字典   RSA公钥编码,在Java和Android中,代码相同,结果不同   java在安卓中实现数字检测语音识别   java取消选择复选框   java如何在其他配置中重用Maven配置XML片段   java有没有一种有效的方法来检查HashMap是否包含映射到相同值的键?   spring处理程序调度失败;嵌套的例外是java。lang.NoClassDefFoundError:org/apache/http/client/HttpClient   带有ehcache的java多层缓存   java如何访问chromium(或任何其他浏览器)cookie   java通过将两个集合与spring data mongodb data中的条件合并来获取计数   安卓中R.java的语法错误