Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress Unit testing help

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #172102
    gilgimech
    Participant

    Does anyone know a good tutorial for setting up the WordPress test environment using the phpUnit method? I’ve tried to follow the few tutorials I’ve found and it seems that they’re out of date or something is missing from the tutorial that the author assumes i know, but I don’t.

    One of the things I don’t get. is where I should set this up. I have a development WordPress installation and that’s where I want to test. None of the tutorials I’ve found covers this scenario. I’ve tried to set it up there but I can’t get it to work.

    #172106
    __
    Participant

    One of the things I don’t get. is where I should set this up.

    Same computer as the WP install you are testing.

    I have a development WordPress installation and that’s where I want to test … I’ve tried to set it up there

    “there” where?

    Does anyone know a good tutorial…?

    Have you read this?

    I’ve never done this myself. I don’t use WP. That was the top search result, and it seemed clear and authoritative.

    but I can’t get it to work.

    “doesn’t work” is useless information. It doesn’t tell us anything useful. (I am not trying to be insulting.) Think about it: if it did work, you wouldn’t be here asking for help. So, if you’ve run into specific problems, please describe what you have actually tried so far, and what went wrong —then we can go from there.

    #172107
    gilgimech
    Participant

    Same computer as the WP install you are testing.

    Right?, but where on that computer? In the root of the WP install? In a separate directory in the WP install? In the root of the theme? In the root of the plugin. In a separate folder altogether?

    All the tutorials I’ve seen tell you to make a new directory for the tests, but they don’t say how to hook it to your WP install or if you should set up an instance of WP in that folder.

    #172108
    __
    Participant

    Right?, but where on that computer?

    http://phpunit.de/manual/current/en/installation.html

    Strictly speaking, where doesn’t exactly matter, provided you remember where you put it (so you can call it later). If you are not doing this on Linux, it gets more complicated. You’ll have to do some research; I can’t help you there.

    All the tutorials I’ve seen tell you to make a new directory for the tests

    WP unit tests are included in the wordpress-develop repo (step 2). You don’t need to worry about it.

    If you’re trying to test an existing WP install, it might have a tests/ directory already: you should check.

    #172109
    gilgimech
    Participant

    un-traq-ed, thanks for the advice, but I think since you don’t develop in WP you might not understand where my confusion is.

    The repo in step 2 isn’t just the tests. It the tests plus four other directories and config files. Which one folder contains a the whole WP app. Should I be using that WP version and run WP from a sub directory or is it just there to have a copy of WP?

    I’ve seen themes and plugins that contain a test folder. That’s partially why I’m confused. Do I add some of the repo files and folders to the WP root and just add the tests folder to each plugin and theme or do I have to install a new instance of the whole repo for each plugin and theme. Then when I run the tests. Do I run them from the WP install root or from the plugin and theme directory roots?

    #172149
    __
    Participant

    Should I be using that WP version and run WP from a sub directory or is it just there to have a copy of WP?

    The tests are for that particular version of WP. You can copy that folder into your existing WP install, and if it is the same version, you should see the same results when you run the tests.

    I’ve seen themes and plugins that contain a test folder. That’s partially why I’m confused. Do I add some of the repo files and folders to the WP root and just add the tests folder to each plugin and theme or do I have to install a new instance of the whole repo for each plugin and theme

    Tests in themes/plugins should be just for that particular theme/plugin. Leave them where they are.

    Then when I run the tests. Do I run them from the WP install root or from the plugin and theme directory roots?

    You run them “from” your command line.

    ~$  phpunit path/to/WP/tests/name-of-test.php
    

    or

    ~$  phpunit path/to/WP/your_plugin/tests/name-of-test.php
    

    The WP handbook says that it is set up to run the entire suite of tests automatically from the root of the repo.

    ~$  cd path/to/your/WP
    ~$  # might want to check you're in the right place
    ~$  ls # if you see the "tests" folder listed below, you're in the right place.
    ~$  # run all tests
    ~$  phpunit
    
    #172186
    gilgimech
    Participant

    un-traq-ed, again thank you for trying, but stop. Just stop.

    Your not answering my questions. You just repeating from instructions I’ve already read, but don’t help.

    I understand you’re trying to help and I do appreciate that, but please just leave it alone, so someone experienced with WordPress testing doesn’t think I’ve gotten my answers.

    This is something I really need to get working.

    #172258
    MrsLannister
    Participant

    Hey,

    I don’t know if this will help, but I have been having the same issues and this is what I have figured out so far. I don’t know how “correct” it is, but I’m at least making progress.

    I’m using XAMPP on windows. The testing site is a different install from the one you already have.

    First, I installed github from this site: https://windows.github.com/

    From the github shell (it puts a link to the shell on the desktop) I did: git clone git://develop.git.wordpress.org/ wordpress

    That put the wordpress unit testing core into my Documents/GitHub folder. I copied that (the whole contents of that wordpress folder that GitHub downloaded) to C:/XAMPP/htdocs/wordpress_testing_site/

    From the XAMPP control panel, I clicked the “admin” button for mySQL. That opened phpMyAdmin. I logged in and created a new database called wordpress_testing

    I opened wp-tests-config-sample.php (in C:/XAMPP/htdocs/wordpress_testing_site) and put in the name and password for mySQL and the name of the database I just created.

    Now, here is the tricky part that took me all day to figure out:

    From the XAMPP control panel, I clicked the button that said “Shell.” When the shell opens up, it is at the XAMPP directory. From there I did cd htdocs/wordpress_testing_site to take me to the root directory of the testing site.

    While in that directory in shell, I ran “phpunit.” You just type “phpunit” in the shell window and hit enter. If you have done everything right it will spend about 3 – 5 minutes spitting out all sorts of information.

    During that process it creates all of the tables for the testing site database. It will also tell you that some assertions failed, but you can ignore this, since you are not trying to test the wordpress core.

    Next, put a copy of your plugin in the testing site’s plugin directory. The file path should be something like c:/XAMPP/htdocs/wordpress_testing_site/src/wp-content/plugins/your_plugin_directory/

    Go to this site and download the zip file and extract its contents into your plugin’s directory on your test site: https://github.com/tierra/wordpress-plugin-tests

    This should put a tests directory inside your plugin directory. Go into that directory and edit the file called bootstrap.php and change the “active_plugins” setting to your plugin’s main php file.

    From the XAMPP shell, cd to your plugin directory and run “phpunit.”

    If you have setup any assertions in your plugin, they should run and it should tell you if any failed.

    What I have yet to figure out is how to see what assertions passed or failed. I’ll let you know if I figure that out.

    If you have any questions, let me know. It took me all freaking day to figure out just this much. The tutorials are really hard to follow when you have never done it before. There are a lot of steps they don’t tell you about!

    #172260
    MrsLannister
    Participant

    PS. I think what was most confusing for me on this is that I kept trying to get to a wordpress front end site that I could navigate, but that apparently isn’t how this works. This isn’t a site for viewing pleasure. Your interactions with it are all through the shell, I think. I’m sure someone will correct me if I’m wrong.

    #172263
    __
    Participant

    phpunit is run from a shell, yes. Originally, I thought the issue was not being familiar with how phpunit is used; and I still believe that this is a significant part of the confusion.


    @gilgimech
    , hopefully, between all this input, you’ve got enough to clear things up. Let us know how it goes.

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘Back End’ is closed to new topics and replies.