How to run Apple Hardware Test on older Mac I have replaced original hard drive to samsung SSD and upgraded memory too.
I have now installed El Capitan and want to run Apple Hardware Test.
I have already tried 'D' , Option + 'D' both didn't do anything.
I have checked under "/System/Library/CoreServices" and no ".diagnostics" folder. I tried to copy this folder to CoreServices and not able to do that since error message says no permission.

sudo cp -R /Volumes/AppleHardwareTest/System/Library/CoreServices/.diagnostics /System/Library/CoreServices

WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type "man sudo" for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password:
cp: /System/Library/CoreServices/.diagnostics: Operation not permitted
cp: /Volumes/AppleHardwareTest/System/Library/CoreServices/.diagnostics: unable to copy extended attributes to /System/Library/CoreServices/.diagnostics: Operation not permitted
cp: /System/Library/CoreServices/.diagnostics/diags.efi: No such file or directory
cp: /System/Library/CoreServices/.diagnostics/Mac-F42386C8: No such file or directory
I followed 'Drew Reece' instructions below which was given in apple discussion forum. https://discussions.apple.com/message/27529118#27529118
and it worked fine for me without messing my machine. even AHT test passed in loop mode, still sometimes I get freeze while using safari and only option to recover is to restart the machine.
Not sure whether AHT test is through and I have no hardware issue or its El Capitan issue on my mid 2007 24" iMac.

Grab the AHT disk image from Apple.com, github has a list be sure you have the correct model.
https://github.com/upekkha/AppleHardwareTest
In Terminal view the output of these two lines…
sysctl hw.model | awk '{ print $2 }'
ioreg -l | grep board-id | awk -F\" '{ print $4 }'
… and compare to the github list

Inside an admin account (it needs to use sudo for 'blessing' the system).
Grab my script, open the ZIP file. In the folder that is created (probably called 2e5eed7dbfbd5dd7e929-6ddeffa80174801842f729c8657e3c460e0ed540 !) drag that script onto Terminal. Then drag the AHT disk image onto Terminal &amp; finally drag the destination disk (a USB disk formatted to Macintosh OS Extended).

It should look a bit like…

[~/]$ /Users/USERNAME/Downloads/2e5eed7dbfbd5dd7e929-6ddeffa80174801842f729c8657e3c460e0ed540/AHT-my- disk.sh /Users/USERNAME/Downloads/AHTdiskimage.dmg /Volumes/path-to-USB

NB: The bold parts will be specific to your system, there are spaces between the file paths.
If you are unsure paste the command here &amp; we can try to confirm it is correct, Terminal correctly escapes spaces &amp; other special characters in file paths if you use drag &amp; drop, so it should complete them correctly for you.

Hit return &amp; it should ask for confirmation and then copy over the AHT disk &amp; offer to bless the tool for use on next boot.

I haven't tested this on 10.11 &amp; I cannot do so at present, sorry. If it fails it is possible to copy over by hand, but that takes a lot of describing. The script just uses the cp command to copy the relevant files over after mounting the disk image. The USB destination is not erased, but avoid copying it to a disk that has valuable data (just in case, have a backup).

It may also appear as a bootable disk in the 'boot picker' (hold alt at startup), this seems to vary on some models.