简体   繁体   English

如何对Python脚本进行集成测试?

[英]How to do integration tests for a Python script?

I have a Python command line script that connects to a database, generates files from the data and sends e-mails. 我有一个Python命令行脚本,它连接到数据库,从数据生成文件并发送电子邮件。 I already have some unit-tests for the important components. 我已经对重要组件进行了一些单元测试。 Now I'd like to do tests that use several or all components together, load the test database with sample data and check for the correct output. 现在我想做一些使用几个或所有组件的测试,用测试数据加载测试数据库并检查输出是否正确。

Are there Python libraries that support this kind of testing? 是否有支持此类测试的Python库? Specifically, I'm looking for easy ways to 具体来说,我正在寻找简单的方法

  • put sample data in the database 将样本数据放入数据库中
  • check for specific changes in the database 检查数据库中的特定更改
  • check for existence and content of specific files 检查特定文件的存在和内容

Should I even do these tests in Python or should I just write a bunch of shell scripts? 我是否应该在Python中进行这些测试,还是应该编写一堆shell脚本?

Yes. 是。 The unittest libraries can be used for this. unittest库可用于此目的。

Don't be fooled by the name. 不要被这个名字所迷惑。 A "unit" is not always a single, standalone class (or function). “单元”并不总是单个独立类(或函数)。

A "unit" can be a composite "unit" of one or more classes or modules or packages. “单元”可以是一个或多个类或模块或包的复合“单元”。

I use nosetests 我用鼻试

http://somethingaboutorange.com/mrl/projects/nose/1.0.0/ http://somethingaboutorange.com/mrl/projects/nose/1.0.0/

And it fits very well with another component, "Coverage", that provides you with information about how much of your code is tested. 它非常适合另一个组件“Coverage”,它为您提供有关测试代码量的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM