How to Filter PHPUnit tests

We have started writing test in our application in the previous article. In you have noticed, when we run the vendor/bin/phpunit tests command, all of our test were running. In a small application, there are few number of tests and it won’t be a problem. But, as the system grows, the number of tests also grows. Luckily, we can filter the tests.

Using Filename

In this article, we see how we can filter the different tests we have written. Let’s start by running a single test filtered by filename. To do so, we can run the command:

//syntax
vendor/bin/phpunit directory/filename.php
//example
vendor/bin/phpunit tests/ReceiptTest.php

This will run the single file ReceiptTest.php.

Using Filter

Next, we will use Filter parameter to filter the test. Filter parameter accepts string in the form of Classes, Methods or Namespaces. Look at the example below. The code will only run the testTax method although ReceiptTest contains another method also testTotal().

vendor/bin/phpunit tests --filter=ReceiptTest::testTax

Using XML File

We will filter our test using XML file. XML file contains configurations for our test cases. We can create xml configuration for tests using following command:

vendor/bin/phpunit --migrate-configuration

This will create a phpunit.xml file on the root directory. If the directory already contains a file with the same name, it will back up the old file and create a new configuration file. A newly generated configuration file looks like below:

Now, when we run our test, it will pass with color syntax. Similarly, we don’t need to pass the directory while running test because it is configured in our xml file.

Now, we can filter the specific test by selecting the testsuite name like app or receipt. Let’s check by running the below command:

vendor/bin/phpunit --testsuite=app

OR

vendor/bin/phpunit --testsuite=app --filter=testTax

When I run the second command, it will give the following output.

These are the basic methods for filtering test cases. It becomes extremely helpful when the system is large and we need to test hundreds or even thousands of test cases.

Read More Articles

20 COMMENTS

  1. Thank you for your website post. Thomas and I are already saving to buy a new
    e-book on this subject matter and your writing has made us to save the money.
    Your notions really responded all our questions. In fact, over what we had acknowledged just before we ran into your great blog.
    My partner and i no longer have doubts and a troubled mind
    because you have totally attended to our own needs in this article.

    Thanks

    Also visit my web page skin care chemical

  2. Right here is the right web site for anybody who hopes to understand this topic.
    You understand a whole lot its almost hard to argue with you (not that I really will need
    to?HaHa). You definitely put a fresh spin on a subject that
    has been written about for ages. Wonderful stuff, just wonderful!

    My site … ultrametabolism diet (Dallas)

  3. What you said made a ton of sense. However, think on this, suppose you
    wrote a catchier title? I mean, I don’t wish to tell you how to run your
    website, however suppose you added a headline that grabbed a person’s attention?
    I mean How to Filter PHPUnit tests – Kodementor is a little plain. You ought to look at Yahoo’s front page and see how they
    write post headlines to get viewers interested. You might try adding a video or a related pic or two to get people excited about what you’ve got to say.
    Just my opinion, it could bring your blog a little bit more interesting.

    Also visit my web site; oil pulling saves teeth – https://bbs.yunweishidai.com/forum.php?mod=viewthread&tid=2259731

  4. We’re a bunch of volunteers and opening a new scheme in our community.
    Your web site offered us with helpful information to paintings on. You’ve performed an impressive task and our entire community can be grateful to you.

    my blog post – try hemp

  5. Hello just wanted to give you a quick heads up.

    The text in your post seem to be running off the screen in Chrome.
    I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought I’d
    post to let you know. The style and design look great though!
    Hope you get the issue solved soon. Cheers

    my blog :: south beach diet

  6. Fantastic beat ! I wish to apprentice while you
    amend your website, how can i subscribe for a blog website?
    The account helped me a appropriate deal. I had been tiny bit familiar
    of this your broadcast offered bright clear concept

  7. Hey there just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.|

LEAVE A REPLY

Please enter your comment!
Please enter your name here