Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is there a best practice for calling language-native functions when writing testable code?

I have experimented a little with php code and have come up with two methodologies:

  • create a wrapper class for all native functions and mocking this wrapper when writing tests
  • using "namespace magic": calling all native functions without specifying the global namespace (aFunction in stead of \aFunction) and then writing a method stub in the given namespace during testing, which will result in the code to be tested using the stub instead of the native function
Is there a more commonly used approach? What are the benefits and shortcomings of the most commonly used methodologies to test language-native function calls?

share|improve this question
Your question started out as a good one, until you asked us to List All the Things™. See programmers.stackexchange.com/faq#dontask, especially the part that says your answer is provided along with the question, and you expect more answers: “I use ______ for ______, what do you use?” –  Robert Harvey Jun 3 '13 at 21:39
Does it differ from language to language is a big list. How could it not be so? –  Robert Harvey Jun 3 '13 at 22:55
@RobertHarvey: point taken. I'll take that out. –  DudeOnRock Jun 3 '13 at 22:55
Use a language where those functions aren't magic, or otherwise different from user-defined functions. Then you can just do whatever you do with your own stuff ;-) –  delnan Jun 4 '13 at 6:37

2 Answers 2

up vote 2 down vote accepted

You don't unit-test language native functions. You test your code. You rely on the language you're using as well as on any third party library.

Library / Language Code

If you want the external code to be tested, that's ok, but not within your project. Fork the third party software, and do the tests there, be it PHP or a mailer class or whatever. Many projects come with unit tests nowadays, so likely there already is something for you to build on.

The reason is simple. As soon as you fix something in a third party software, you immediately loose the ability to update, and thus the greatest benefit from using that software. Forking into your own (sub) projects works like a proxy, where you can change the software, but still are able to update (e.g. using git rebase), and, as an extra, you can easily contribute back to the projects, you rely on.


If you - like you stated in your comment - want to make FTP (or MySQL or whatever) testable, i.e., get independend from PHP resources, encapsulate them in their own classes. If you use an FTP class like

class FTP

    public function connect($server, $user, $pass)

    public function put($bytes)


in combination with Dependency Injection, you can easily mock it. The class itself is usually not unit-tested - it is subject of an integration test within a controlled environment.

share|improve this answer
I think you misunderstood my question. I am not asking how to test third party code, I am asking how to test my code that uses language native functions. Example: My class interacts with an ftp server with ftp_connect and ftp_put. How do I best mock those function calls, so I can test my code in isolation, without having to rely on an actual ftp resource. –  DudeOnRock Jun 4 '13 at 16:08
Going to extend my answer accordingly. –  nibra Jun 4 '13 at 17:12

Generally you don't want to mock the native functions. You should rely on them to do their thing. Though there are the exceptions like (in php: ftp_connect, time, etc)

You have a couple of options.

1) You create a wrapper class that wraps the native function calls so that you are able to control it. The downside to this is that you are still needing to possibly call an actual resource if you wanted to test your wrapper. Or you end up with a location of untested code.

2) As you state with doing the namespace magic (specific to PHP). I like this method as it allows you to control the native function even with having a wrapper class.

If the functionality has a native class interface, use that instead as you would be able to create a mock. In other languages, you are also able to overload native functions which would allow to create a mock of the function in question.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.