将数据传递到 Python Unittest 重定向 STDIN 或 Pickle 中哪种更好?
简短问题
将数据输入 Python 单元测试用例的最佳方法是什么?
背景
我的项目使用 Python 的 unittest 模块作为执行一系列测试的自动化方式,这些测试需要在许多相同类型的板上运行。到目前为止,这非常适合单元测试模块的设计目的;不同的是,每个测试用例都需要知道运行特定信息以存储在 Django 数据库中。
需要传入的数据包括序列号、测试板的人员、日期以及其他此类性质的信息。值得注意的是,测试板的顺序是由从盒子中取出板 X 的人选择的,因此不可能预测序列号。
想法
目前,我正在通过 pickle 将所需的数据传入和传出测试用例。这种方法在小型测试中工作得很好,但我担心的是读取和写入同一个文件 100k 次以上会给数据损坏带来很大的空间(+它不是那么快)。我写了一个 SO Question 的答案,它以我认为也适合此应用程序的方式重定向标准输入。
下一步将围绕这些测试封装一个 GUI。个人目标是能够通过命令行运行测试,然后让 GUI 调用相同的命令行函数。出于这个原因,我倾向于转向重定向的标准输入。
系统/部署信息
所需操作系统支持:Windows XP 和 Windows 7
理想的操作系统支持:Mac OS X 和 Linux
Python 版本:2.7
任何想法或评论将不胜感激。
Short Question
What is the best way to get data into a Python unittest case ?
Background
My project is using Python's unittest module as an automated way execute a series of tests that will need to run on many of the same type of boards. So far this is a good fit to what the unittest module was designed for; the twist is that each test case needs to know run specific information to store in a Django database.
The data that needs to be passed in includes a serial number, who tested the board, the date, and other things of this nature. It is worth noting that the order in which the boards will be tested is chosen by a human that pulls board X from a box, so predicting the serial number is not possible.
Thoughts
Currently, I am passing the required data to and from the test cases via pickle. This method works fine in small testing, but my concern is reading and writing to the same file 100k + times gives lots of room for data corruption (+ it's not that fast). I wrote an answer to a SO Question that redirects the stdin in a way that I think might work well for this application as well.
The next step is going to be wrap a GUI around these tests. A personal goal would be to have the ability to run the tests via command line then have the GUI call the same command line functions. For this reason I am leaning towards moving to the redirected stdin.
System / Deployment Information
Required OS Support: Windows XP and Windows 7
Ideal OS Support: Mac OS X and Linux
Python Version: 2.7
Any thoughts or comments would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我创建了针对第三方服务(Zoho CRM)进行测试的单元测试。要测试服务 API,您需要存储用户名和密码凭据。
由于这是优质服务并且您正在创建开源软件,因此您自然不能将登录凭据硬编码到源代码本身。
所以我最终使用了环境变量 - 工作得很好:
这是示例:
https://github.com/miohtama/mfabrik.zoho/blob/master/mfabrik/zoho/tests.py
作为一个更大的问题,我认为尝试强制单元测试模块做一些事情是一开始就不应该做的事情并不是一个好主意。也许您应该尝试编写自己的单元测试运行程序,它将在某处进行必要的准备(提取信息、存储结果)。
I have created unittests which test against a third party service (Zoho CRM). To test the service API, you need to store username and password crendetials.
Since this is premium service and you are creating open source software, naturally you cannot hardcode your login credentials to the source code itself.
So I ended up using environment variables - work quite well:
Here is the example:
https://github.com/miohtama/mfabrik.zoho/blob/master/mfabrik/zoho/tests.py
As a bigger problem I think trying to enforce unit tests module to do something it was not supposed to do in the first place is not a good idea. Maybe you should try to write your own unit test runner which would do necessary preparements (pulling info, storing results) somewhere.