<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Theforce</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Theforce"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Theforce"/>
	<updated>2026-06-11T12:54:01Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=User:Luke_Hudson&amp;diff=20457</id>
		<title>User:Luke Hudson</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=User:Luke_Hudson&amp;diff=20457"/>
		<updated>2008-04-10T04:57:04Z</updated>

		<summary type="html">&lt;p&gt;Theforce: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I&#039;m one of the developers in the eLearning team at Catalyst IT.&lt;br /&gt;
&lt;br /&gt;
I&#039;ve been working on Moodle development for nearly two years now, working on a large variety of tasks.&lt;br /&gt;
&lt;br /&gt;
I was born in 1977 in London, but moved to New Zealand about twenty years ago.&lt;br /&gt;
&lt;br /&gt;
I speak French reasonably fluently, as a second language, and I&#039;ve learned some German and New Zealand Sign Language.&lt;/div&gt;</summary>
		<author><name>Theforce</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=User_talk:Luke_Hudson&amp;diff=23480</id>
		<title>User talk:Luke Hudson</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=User_talk:Luke_Hudson&amp;diff=23480"/>
		<updated>2006-08-23T05:32:37Z</updated>

		<summary type="html">&lt;p&gt;Theforce: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I&#039;m one of the newer developers at Catalyst IT.  We do a lot of Moodle devlopement, including customized versions of Moodle for clients, and bugfixes / new features make their way back to the Moodle community.&lt;br /&gt;
&lt;br /&gt;
I&#039;m currently working on Moodle as well as Elgg.&lt;/div&gt;</summary>
		<author><name>Theforce</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Unit_test_API&amp;diff=3938</id>
		<title>Unit test API</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Unit_test_API&amp;diff=3938"/>
		<updated>2006-08-23T05:30:58Z</updated>

		<summary type="html">&lt;p&gt;Theforce: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The purpose of unit testing is to test the individual parts of the program (functions, and methods of classes) to make sure that each individually does the right thing. Once unit testing has given you the confidence that each part works, then you should use other forms of testing to ensure that the different parts work together properly.&lt;br /&gt;
&lt;br /&gt;
The unit testing framework is based on the [http://www.lastcraft.com/simple_test.php SimpleTest] framework. It was incorporated into Moodle by Nick Freear and Tim Hunt from [http://www.open.ac.uk/ The Open University].&lt;br /&gt;
&lt;br /&gt;
== Running the unit tests in Moodle ==&lt;br /&gt;
&lt;br /&gt;
=== Running the basic tests ===&lt;br /&gt;
&lt;br /&gt;
# Log in with an admin account. &lt;br /&gt;
# Go to the admin screen.&lt;br /&gt;
# Click on the &#039;&#039;&#039;Reports&#039;&#039;&#039; link near the bottom of the page.&lt;br /&gt;
# Click on the &#039;&#039;&#039;Run the unit tests&#039;&#039;&#039; link.&lt;br /&gt;
# Wait for the tests to run.&lt;br /&gt;
&lt;br /&gt;
=== Options for running the tests ===&lt;br /&gt;
&lt;br /&gt;
At the bottom of the tests page, there is form that lets you adjust the options used when running the tests.&lt;br /&gt;
&lt;br /&gt;
==== Show passes as well as fails ====&lt;br /&gt;
&lt;br /&gt;
Normally, only details of the tests that have failed are printed. Turning on this options shows details of all the passes too.&lt;br /&gt;
&lt;br /&gt;
==== Show the search for test files ====&lt;br /&gt;
&lt;br /&gt;
The tests to run are found automatically be searching the codebase for files whose names match &#039;&#039;&#039;test*.php&#039;&#039;&#039; in directories called &#039;&#039;&#039;simpletest&#039;&#039;&#039;. Turning on this option will print a list of the folders searched and the test files found. This is sometimes useful for debugging.&lt;br /&gt;
&lt;br /&gt;
This option is particularly useful when one of your test files has a syntax error. When this happens, you sometimes just get a blank page with no error message. Turning on the show search option lets you see which test file it was that gave the error. If necessary, you can enable this option manually by adding &amp;quot;showsearch=1&amp;quot; to the end of the URL.&lt;br /&gt;
&lt;br /&gt;
==== Run a thorough test (may be slow) ====&lt;br /&gt;
&lt;br /&gt;
If you turn on this option, then as well as looking for files called &#039;&#039;&#039;test*.php&#039;&#039;&#039;, the search also looks for files called &#039;&#039;&#039;slowtest*.php&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
To be useful, the full test run should find most bugs, but not take too long to complete. So if you have very, very detailed tests of an area of the code, it may be better to select a subset for everday testing, and only use the more detailed tests when a bug is reported, or you are doing new development in that area of the code.&lt;br /&gt;
&lt;br /&gt;
This option is most useful when combined with the next option.&lt;br /&gt;
&lt;br /&gt;
==== Only run tests in ====&lt;br /&gt;
&lt;br /&gt;
Normally, tests from all parts of the codebase are run. However, when you are just doing development of one part of the code, that is a waste of time. You can type the name of a folder (for example &#039;&#039;&#039;mod/quiz&#039;&#039;&#039;) or a particular test file (for example &#039;&#039;&#039;lib/simpletest/testdatalib.php&#039;&#039;&#039;) and then only those tests will be run.&lt;br /&gt;
&lt;br /&gt;
[[Image:RunOnlyTheseTests.png|right]] Instead of typing a path into this box, there is an easier way. Whenever a pass or fail is displayed, the name of the test file is printed. Each section of the path name is a link to run only the tests in that folder or file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Writing new tests ==&lt;br /&gt;
&lt;br /&gt;
As an example, suppose we wanted to start writing tests for the functions in the file &#039;question/editlib.php&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Where to put the tests ===&lt;br /&gt;
&lt;br /&gt;
If you have read the first half of this page and were paying attention, you can probably work out that you should create a folder called &#039;&#039;&#039;question/testeditlib&#039;&#039;&#039;, and create a file in there called something like &#039;&#039;&#039;testeditlib.php&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
The skeleton of this file should look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Unit tests for (some of) question/editlib.php.&lt;br /&gt;
 *&lt;br /&gt;
 * @copyright &amp;amp;copy; 2006 The Open University&lt;br /&gt;
 * @author T.J.Hunt@open.ac.uk&lt;br /&gt;
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License&lt;br /&gt;
 * @package question&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/** */&lt;br /&gt;
require_once(dirname(__FILE__) . &#039;/../../config.php&#039;);&lt;br /&gt;
&lt;br /&gt;
global $CFG;&lt;br /&gt;
require_once($CFG-&amp;gt;libdir . &#039;/simpletestlib.php&#039;); // Include the test libraries&lt;br /&gt;
require_once($CFG-&amp;gt;dirroot . &#039;/question/editlib.php&#039;); // Include the code to test&lt;br /&gt;
&lt;br /&gt;
/** This class contains the test cases for the functions in editlib.php. */&lt;br /&gt;
class question_editlib_test extends UnitTestCase {&lt;br /&gt;
    function test_get_default_question_category() {&lt;br /&gt;
        // Do the test here/&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That is, you have a class called something_test, and in that class you have lots of methods called test_something. Normally, you have one test method for each function you want to test, and you may as well called the test method &#039;&#039;&#039;test_name_of_function_being_tested&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
=== Inside a test function ===&lt;br /&gt;
&lt;br /&gt;
The inside of a test function tyically looks like this: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;function test_get_default_question_category() {&lt;br /&gt;
    // Set things up in preparation for the test.&lt;br /&gt;
&lt;br /&gt;
    // Call the function you want to test.&lt;br /&gt;
&lt;br /&gt;
    // Check that the result is what you expected.&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
=== Test data ===&lt;br /&gt;
&lt;br /&gt;
TODO&lt;br /&gt;
&lt;br /&gt;
=== setUp and tearDown methods ===&lt;br /&gt;
&lt;br /&gt;
If all your test cases relate to the same area of code, and so need the same set of test data, then you can create a method called &amp;lt;code&amp;gt;setUp()&amp;lt;/code&amp;gt; that sets up the test data. If present, this method will be called before each test method. You can write a matching &amp;lt;code&amp;gt;tearDown()&amp;lt;/code&amp;gt; method if there is any clean-up that needs to be done after each test case has run.&lt;br /&gt;
&lt;br /&gt;
If you have some test test cases the need one sort of setup, and some other test cases that need a different setup, consider splitting your tests into two separate classes, each with its own &amp;lt;code&amp;gt;setUp()&amp;lt;/code&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
=== Further information ===&lt;br /&gt;
&lt;br /&gt;
The simpletest documentation is at: http://simpletest.sourceforge.net/.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Changes to your existing code to make it work with unit testing ==&lt;br /&gt;
&lt;br /&gt;
When code is being tested, it gets included from inside one of the simpletest library function. If the code is expecting to be run directly (for example, if it is a view.php or index.php function), you are likely to get errors becuase that expectation is no longer true.&lt;br /&gt;
&lt;br /&gt;
=== Include paths ===&lt;br /&gt;
&lt;br /&gt;
Includes like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(&#039;../../config.php&#039;); // Won&#039;t work.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
won&#039;t work. Instead, the more robust option is &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(dirname(__FILE__) . &#039;/../../config.php&#039;); // Do this.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Access to global variables ===&lt;br /&gt;
&lt;br /&gt;
Because your code was included from within a function, you can&#039;t access access global variables until you have done a global statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(dirname(__FILE__) . &#039;/../../config.php&#039;);&lt;br /&gt;
require_once($CFG-&amp;gt;libdir . &#039;/moodlelib.php&#039;); // Won&#039;t work.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;require_once(dirname(__FILE__) . &#039;/../../config.php&#039;);&lt;br /&gt;
&lt;br /&gt;
global $CFG; // You need this.&lt;br /&gt;
require_once($CFG-&amp;gt;libdir . &#039;/moodlelib.php&#039;); // Will work now.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== More about unit testing in general ==&lt;br /&gt;
&lt;br /&gt;
The best book I know about unit testing is &#039;&#039;&#039;Pragmatic Unit Testing in Java with JUnit&#039;&#039; by Andrew Hunt (no relation) and David Thomas. I know, this book is not called Pragmatic Unit Testing in PHP with SimpleTest. However, it is an excellent book - short, to the point, and very practical. Most of what it says is not specific to Java and JUnit and it is obvious how to apply it in our testing setup.&lt;/div&gt;</summary>
		<author><name>Theforce</name></author>
	</entry>
</feed>