English 中文(简体)
我怎么能够测试一个执行指挥线方案的PHP级方法?
原标题:How can I unit test a PHP class method that executes a command-line program?

对于PHP应用程序I m Developing,我需要阅读目前对SHA的轮值修订,当然,我可以通过使用壳体/外壳或背标语来实施该轮指挥线客户。

我显然把这一呼吁变成了自己的一种方法,以便我能够轻易地孤立起来,为我的单位测试的其余部分进行模拟。 因此,我的班子看着这样的情况:

class Task_Bundle
{

    public function execute()
    {
        // Do things
        $revision = $this->git_sha();
        // Do more things
    }

    protected function git_sha()
    {
        return `git rev-parse --short HEAD`;
    }
}

Of course, although I can test most of the class by mocking git_sha, I m struggling to see how to test the actual git_sha() method because I don t see a way to create a known state for it. I don t think there s any real value in a unit test that also calls git rev-parse to compare the results? I was wondering about at least asserting that the command had been run, but I can t see any way to get a history of shell commands executed by PHP - even if I specify that PHP should use BASH rather than SH the history list comes up empty, I presume because the separate backticks executions are separate terminal sessions.

我很想知道,我会如何检验,或者说,在将来维持这种评估时,是否只是让这种方法未经测试和加以谨慎?

最佳回答

尽管你接受了答案,但请允许我谈谈“单位测试”应当如何发挥作用,因为即使所展示的解决办法确实奏效,而且确实是空洞的,它还是有一些倒退。

首先,你们不想实际执行你的指挥,因为这将测试外部软件。 从此刻起,你依靠外部力量无法控制(如果我想要用手法测试你的软件,情况如何? 我是否必须排入黑板或任何 g。 你真的想要做的是,指挥线指挥是否正确,执行得很好。

www.un.org/spanish/ecosoc 正如我以前所反对的那样,这里是我所说的: 我设立了一间执行炮弹的班子。 在我想要与伤者互动的任何地方,我都投下了这门课堂并使用它。 因此,为了测试,我可以 mo弄这个类别,看看是否执行正确的方法,是否确定了正确的参数。

这有许多好处:

  • I have one point of interacting with the shell. If anything changes in the future or if I have the need to do anything before executing the command, I can add the behaviour to the class. The PHP function is encapsulated nicely.
  • It works on every PC, even if the shell_exec command does not work for any reason. The tests get executed on every pc (development machines, testserver, integration server) without the need to worry about external dependencies.

当然,为了验证您的软件作为一个整体运行,你必须进行不模拟的一体化测试。 但这完全不同。 考试应保证你的班级(或方法)能做所需的工作。 有时,这并不明确,因为你可能决定在某些地方使用真正的类别,因为模拟会导致大量间接费用。 但是,就Im而言,如果它依赖外部图书馆/软件,可能由于某种原因无法安装或无法运行,它就不再以任何手段进行单位测试。

问题回答

至少,你可以做一些事情,例如检查重新布局,以便与......(abcdef0-9){7}美元匹配。

如果你真心想的话,你可以写一个 du条码/code>,并将它的位置留给你的环境<条码>。 它是否做了任何必要的鉴定,然后产生与实际结果相符的产出。 如果你想要的话,你甚至可以想象会把它推向真正的<代码>git,但你却不能保证结果。

You d have to be careful, though....You d want do just enough so that the class can be fooled into thinking it s really git, but if you make it behave too much like the real thing does, that amounts to writing your own half-assed version of Git.





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签