JsonRPC API模拟器的集合,专为单元测试设计
项目描述
Privex的JsonRPC模拟器
Privex的RPC模拟器是各种使用jsonrpcserver的Python模块,用于模拟特定类型的JsonRPC节点,以便在单元测试中使用。
+===================================================+
| © 2019 Privex Inc. |
| https://www.privex.io |
+===================================================+
| |
| Originally Developed by Privex Inc. |
| |
| Core Developer(s): |
| |
| (+) Chris (@someguy123) [Privex] |
| (+) Kale (@kryogenic) [Privex] |
| |
+===================================================+
简述:安装并使用Privex RPC模拟器
从PyPi安装(详细安装信息请参阅安装)
pipenv install rpcemulator # If you use pipenv (and you should!)
pip3 install rpcemulator # Otherwise, standard pip installation
快速示例使用
from privex.rpcemulator.bitcoin import BitcoinEmulator
from privex.jsonrpc import BitcoinRPC
rpc = BitcoinRPC()
"""
You can interact with the emulated Bitcoin daemon with any JsonRPC library you prefer.
However, privex-jsonrpc comes as a dependency of rpcemulator for convenience :)
"""
with BitcoinEmulator():
print('Balance is:', rpc.getbalance())
print('Network info is:', rpc.getnetworkinfo())
目录(Github README)
注意:以下目录是为Github设计的。链接在PyPi的描述中不起作用,在其他地方阅读此README.md时可能也不起作用。
一般信息
Privex的RPC模拟器是各种使用jsonrpcserver的Python模块,用于模拟特定类型的JsonRPC节点,以便在单元测试中使用。
目前只包含一个模拟器:bitcoin.BitcoinEmulator
,它模拟了一个bitcoind
RPC服务器,允许与bitcoind
(或基于bitcoind的其他节点)交互的代码进行测试,而无需运行币币服务。
这意味着您可以使用像Travis CI这样的持续集成系统测试与bitcoind
交互的代码,在您通常无法运行完整币币服务的情况下。
安装
从PyPi下载和安装
使用Pipenv(推荐)
pipenv install rpcemulator
使用标准的Python pip
pip3 install rpcemulator
(替代)手动从Git安装
选项1 - 使用pip直接从Github安装
pip3 install git+https://github.com/Privex/rpcemulator
选项2 - 克隆并手动安装
# Clone the repository from Github
git clone https://github.com/Privex/rpcemulator
cd rpcemulator
# RECOMMENDED MANUAL INSTALL METHOD
# Use pip to install the source code
pip3 install .
# ALTERNATIVE MANUAL INSTALL METHOD
# If you don't have pip, or have issues with installing using it, then you can use setuptools instead.
python3 setup.py install
文档
本项目的完整文档在上文提供(点击“Read The Docs”图片),包括:
- 如何安装应用程序及其依赖项
- 如何使用各种函数和类
- 模块和类的通用文档,供贡献者参考
如何构建文档
git clone https://github.com/Privex/rpcemulator
cd rpcemulator/docs
pip3 install -r requirements.txt
# It's recommended to run make clean to ensure old HTML files are removed
# `make html` generates the .html and static files in docs/build for production
make clean && make html
# After the files are built, you can live develop the docs using `make live`
# then browse to http://127.0.0.1:8100/
# If you have issues with content not showing up correctly, try make clean && make html
# then run make live again.
make live
许可证
此Python模块由伯利兹城Privex Inc.创建,并按照X11/MIT许可证授权。请参阅LICENSE文件以获取许可证文本。
简短版;许可证
我们不提供任何保修。您可以复制它、修改它、将其用于具有不同许可证的项目中,甚至在商业(付费)软件中。
最重要的规则是 - 您必须在任何副本中保留原始许可证文本可见(参见LICENSE
)。
示例用法
在单元测试中使用JsonRPC模拟器
import unittest
from privex.rpcemulator.bitcoin import BitcoinEmulator
from privex.jsonrpc import BitcoinRPC
class TestMyThing(unittest.TestCase):
emulator: BitcoinEmulator
"""Stores the :class:`.BitcoinEmulator` instance"""
rpc = BitcoinRPC()
"""For this example, we're using our BitcoinRPC class and communicating with the RPC directly"""
@classmethod
def setUpClass(cls) -> None:
"""Launch the Bitcoin RPC emulator in the background on default port 8332"""
cls.emulator = BitcoinEmulator()
@classmethod
def tearDownClass(cls) -> None:
"""Shutdown the Bitcoin RPC emulator process"""
cls.emulator.terminate()
def test_something(self):
"""Run whatever code depends on a Bitcoin RPC"""
self.assertGreater(self.rpc.getbalance(), 0)
在您的代码中使用JsonRPC模拟器,并使用上下文管理器
使用适当的模拟器类,并用with
语句,这样一旦完成查询,服务器就会自动停止。
这可以防止留下任何Web服务器进程的风险。
from privex.rpcemulator.bitcoin import BitcoinEmulator
from privex.jsonrpc import BitcoinRPC
rpc = BitcoinRPC()
print('Starting BitcoinEmulator')
with BitcoinEmulator():
print('Balance is:', rpc.getbalance())
print('Network info is:', rpc.getnetworkinfo())
print('Stopped BitcoinEmulator')
单元测试
单元测试存储在tests/
文件夹中,分为几个test_xxxx
文件。
我们使用Travis CI进行持续集成,每当将新的提交、标签或分支推送到这个Github仓库时,都会运行测试套件。
我们还使用CodeCov,它与我们的Travis CI设置集成,并提供测试覆盖率统计信息,使我们和贡献者可以直观地看到代码中有多少被我们的单元测试覆盖。
简短版;运行测试
pip3 install -r requirements.txt
pytest -v
有关使用单元测试的更多信息,请参阅文档中的如何使用单元测试部分。
贡献
我们乐于接受任何大小的pull请求。
请确保您所做的任何更改都符合以下基本要求
- 从其他项目取出的任何代码应与MIT许可证兼容
- 这是一个新项目,因此,支持3.4版本之前的Python版本优先级非常低。
- 然而,我们乐于接受旨在提高与较旧版本的Python兼容性的PR,只要它不会
- 极大地增加代码的复杂性
- 或者为较新版本的Python用户带来问题。
贡献的法律免责声明
没有人愿意阅读一个充满法律文本的长文档,所以我们在这里总结了重要的部分。
如果您将您创建的/拥有的内容(如代码或文档)贡献给由Privex创建的/拥有的项目,那么您可能自动授予我们不受限制的使用权,无论我们的项目适用的开源许可证如何。
如果您不希望授予我们您的内容不受限制的使用权,请确保将您的内容放在一个单独的文件中,确保在文件的开始处(例如代码注释)或其中(例如名为LICENSE的文件)清楚地显示您的内容的许可证。
您应在您的pull请求或问题中告诉我们您已包含受单独许可证许可的文件,以便我们确保没有可能阻止我们接受您的贡献的许可证冲突。
如果您愿意阅读整个法律文本,它应包含在privex_contribution_agreement.txt
中。
感谢您的阅读!
如果您觉得这个项目对您有帮助,考虑从Privex获取VPS或专用服务器。
价格从每月仅US$8起(我们接受加密货币!)
项目详情
下载文件
下载您平台的文件。如果您不确定该选择哪一个,请了解有关安装包的更多信息。