简体   繁体   English

结构:如何对fabfile进行单元测试?

[英]Fabric: How can I unit test my fabfile?

In the previous project I was working on, our fabfile got out of control. 在我从事的上一个项目中,我们的fabfile失控了。 While the rest of our project was well-tested, we didn't write a single test for our fabfile. 尽管我们项目的其余部分都经过了充分的测试,但我们并未为fabfile编写任何测试。 Refactoring was scary, and we weren't confident a fabric command would work how we expected until we ran the command. 重构令人恐惧,在运行命令之前,我们不确定结构命令是否可以按预期工作。

I'm starting a new project, and I'd like to make sure our fabfile is well-tested from the beginning. 我正在开始一个新项目,我想确保我们的fabfile从一开始就经过了严格的测试。 Obey the Testing Goat has a great article discussing some possible strategies, yet it has more questions than answers. 服从测试山羊有一篇很棒的文章讨论了一些可能的策略,但是问题多于答案。 Using fabtest is a possibility, although it seems to be dead. 尽管似乎已死,但可以使用fabtest

Has anyone successfully unit tested their fabfile? 是否有人成功对fabfile进行了单元测试? If so, how? 如果是这样,怎么办?

  1. run your Fabfile task in a Docker instance Docker实例中运行Fabfile任务

  2. use docker diff to verify that the right files were changed by the Fabfile. 使用docker diff来验证Fabfile是否更改了正确的文件。

This is still quite a bit of work, but it allows testing without excessive Fabfile modifications. 这仍然需要大量工作,但是无需进行过多的Fabfile修改即可进行测试。

Have you tried python-vagrant ? 您尝试过python-vagrant吗? It seems to do the same thing that fabtest does, but it includes some Fabric demos and is still used and maintained. 它似乎和fabtest一样,但是它包含一些Fabric演示,并且仍在使用和维护。

The slides - mentioned by Henrik Andersson - from back then are available here 幻灯片- 亨里克·安德森(Henrik Andersson)提到的 -从那时起可在此处获得

Robin Kåveland Hansen replied to me: RobinKåvelandHansen回答了我:

There are some examples of the types of refactoring that we did in order to keep our fabric code well-tested there. 为了使结构代码在此得到良好测试,我们做了一些重构类型的示例。

In general I would say the best advice is to try avoiding low-level code such as shell commands in higher level code that makes decisions about what code to run, eg. 通常,我会说最好的建议是尝试避免在执行高级代码的情况下,例如高层命令中的shell命令之类的低级代码。 isolate effect-full code from code that makes decisions. 将效果完全的代码与决策的代码隔离开。

Branching increases the amount of test-cases that you need and it's a lot more effort to write good test-cases for code that changes state on some server. 分支增加了所需的测试用例数量,并且为更改某些服务器上的状态的代码编写良好的测试用例需要花费更多的精力。

At the time, we used mock to mock out fabric to write test-cases for branch-less code that has side-effects on the server, so the code + tests would look a lot like this 当时,我们使用模拟来模拟结构,以编写对服务器有副作用的无分支代码的测试用例,因此代码+测试看起来像这样
Obviously this has the weakness that it won't pick up bugs in the shell commands themselves. 显然,这样做有一个缺点,那就是它不会在shell命令本身中拾取错误。 My experience is that this is rarely the cause of serious problems, though. 我的经验是,这很少会引起严重问题。

Other options using mock would be to use the following idea to run the tests locally on your machine instead of remotely 使用模拟的其他选项是使用以下思路在计算机上本地运行测试,而不是远程运行测试

Maybe the most robust approach is to run the tests in vagrant, but that has the disadvantage of requiring lots of setup and has a tendency to make the tests slower. 也许最健壮的方法是在无所事事的环境中运行测试,但这具有需要大量设置的缺点,并且倾向于使测试变慢。
I think it's important to have fast tests, because then you can run them all the time and they give you a really nice feedback-loop. 我认为进行快速测试非常重要,因为这样您就可以一直运行它们,并且它们会为您提供一个非常好的反馈循环。

The deploy-script I've written for my current employer has ~150 test cases and runs in less than 0.5 seconds, so the deploy-script will actually do a self-test before deploying. 我为当前雇主编写的deploy-script具有〜150个测试用例,并且运行时间不到0.5秒,因此deploy-script实际上将在部署之前进行自检。
This ensures that it is tested on each developer machine all the time, which has picked up a good few bugs for example for cases where linux and mac osx behave differently. 这样可以确保始终在每台开发人员机器上对其进行测试,例如在linux和mac osx行为不同的情况下,已经发现了很多错误。

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

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