Most active commenters
  • 1718627440(5)
  • vkou(4)

←back to thread

67 points ingve | 18 comments | | HN request time: 1.4s | source | bottom
1. dherls ◴[] No.45949945[source]
This blog post talks as if mocking the `open` function is a good thing that people should be told how to do. If you are mocking anything in the standard library your code is probably structured poorly.

In the example the author walks through, a cleaner way would be to have the second function take the Options as a parameter and decouple those two functions. You can then test both in isolation.

replies(4): >>45950043 #>>45952302 #>>45952420 #>>45952464 #
2. bluGill ◴[] No.45950043[source]
Details matters, but good test doubles here are important. You want to capture all calls to IO and do something different. You don't want tests to break because someone has a different filesystem, didn't set their home directory as you want it setup, or worse is trying to run two different tests at the same time and the other test is changing files the other wants.

Note that I said test doubles. Mocks are a bit over specific - they are about verifying functions are called at the right time with the right arguments, but the easy ability to set return values makes it easy to abuse them for other things (this abuse is good, but it is still abuse of the intent).

In this case you want a fake: a smart service that when you are in a test setups a temporary directory tree that contains all the files you need in the state that particular test needs, and destroys that when the test is done (with an optional mode to keep it - useful if a test fails to see debug). Depending on your situation you may need something for network services, time, or other such things. Note that in most cases a filesystem itself is more than fast enough to use in tests, but you need isolation from other tests. There are a number of ways to create this fake, it could override open, or it could just be a GetMyProgramDir function that you override are two that I can think of.

replies(2): >>45950429 #>>45950484 #
3. jpollock ◴[] No.45950429[source]
Your tests are either hermetic, or they're flaky.

That means the test environment needs to be defined and versioned with the code.

4. dherls ◴[] No.45950484[source]
Even in the case you mention you really shouldn't be overriding these methods. Your load settings method should take the path of the settings file as an argument, and then your test can set up all the fake files you want with something like python's tempfile package
replies(1): >>45953661 #
5. 1718627440 ◴[] No.45952302[source]
> This blog post talks as if mocking the `open` function is a good thing that people should be told how to do. If you are mocking anything in the standard library your code is probably structured poorly.

Valgrind is a mock of standard library/OS functions and I think its existence is a good thing. Simulating OOM is also only possible by mocking stuff like open.

replies(2): >>45952472 #>>45954495 #
6. sunrunner ◴[] No.45952420[source]
> If you are mocking anything in the standard library your code is probably structured poorly.

I like Hynek Schlawak's 'Don’t Mock What You Don’t Own' [1] phrasing, and while I'm not a fan of adding too many layers of abstraction to an application that hasn't proved that it needs them, the one structure I find consistently useful is to add a very thin layer over parts that do I/O, converting to/from types that you own to whatever is needed for the actual thing.

These layers should be boring and narrow (for example, never mock past validation you depend upon), doing as little conversion as possible. You can also rephrase the general purpose open()-type usage into application/purpose-specific usages of that.

Then you can either unittest.mock.patch these or provide alternate stub implementations for tests in a different way, with this this approach also translating easily to other languages that don't have the (double-edged sword) flexibility of Python's own unittest.mock.

[1] https://hynek.me/articles/what-to-mock-in-5-mins/

7. vkou ◴[] No.45952464[source]
> This blog post talks as if mocking the `open` function is a good thing that people should be told how to do.

It does. And this is exactly the problem, here!

> TFA: The thing we want to avoid is opening a real file

No! No, no, no! You do not 'want to avoid opening a real file' in a test.

It's completely fine to open a real file in a test! If your code depends on reading input files, then your test should include real input files in it! There's no reason to mock any of this. All of this stuff is easy to set up in any unit test library worth it's salt.

replies(1): >>45954615 #
8. vkou ◴[] No.45952472[source]
All rules exist to be broken in the right circumstances. But in 99.9% of test code, there's no reason to do any of that.
replies(1): >>45952605 #
9. 1718627440 ◴[] No.45952605{3}[source]
I think when testing code with an open call, it is a good idea to test what happens on different return values of open. If that is not what you intent to test for this method, then that method shouldn't contain open at all, as already pointed out by other comments.
replies(1): >>45955980 #
10. bluGill ◴[] No.45953661{3}[source]
There are a number of different ways to solve this problem. I too use the path of settings in my code, but I'm not against overriding open and all the other file io functions. Of course this article is about python which has different abilities than other languages, what is best in python is not what is best in other languages, and I'm trying to stay at a higher level that a particular language.
11. paulf38 ◴[] No.45954495[source]
> Valgrind is a mock of standard library/OS functions and I think its existence is a good thing.

That is mostly wrong.

Valgrind wraps syscalls. For the most part it just checks the arguments and records any reads or writes to memory. For a small number of syscalls it replaces the syscall rather than wrapping it (for instance calls like getcontext where it needs to get the context from the VEX synthetic CPU rather than the real CPU).

Depending on the tool it can also wrap or replace libc and libpthread functions. memcheck will replace all allocation functions. DRD and Helgrind wrap all pthread functions.

replies(1): >>45954939 #
12. 9rx ◴[] No.45954615[source]
> then your test should include real input files in it! There's no reason to mock any of this.

That's okay for testing some branches of your code. But not all. I don't want to have to actually crash my hard drive to test that I am properly handling hard drive crashes. Mocking[1] is the easiest way to do that.

[1] For some definition of mock. There is absolutely no agreement found in this space as to what the terms used mean.

13. 1718627440 ◴[] No.45954939{3}[source]

    $ cat test.c
    void main (void) {
      malloc (1000);
    }
    
    $ make test
    cc     test.c   -o test
    
    $ valgrind --leak-check=full --show-leak-kinds=all -s ./test
    Memcheck, a memory error detector
    Command: ./test
    
    HEAP SUMMARY:
        in use at exit: 1,000 bytes in 1 blocks
      total heap usage: 1 allocs, 0 frees, 1,000 bytes allocated
    
    1,000 bytes in 1 blocks are still reachable in loss record 1 of 1
       at 0x483877F: malloc (vg_replace_malloc.c:307)
       by 0x109142: main (in test.c:2)
    
    LEAK SUMMARY:
       definitely lost: 0 bytes in 0 blocks
       indirectly lost: 0 bytes in 0 blocks
         possibly lost: 0 bytes in 0 blocks
       still reachable: 1,000 bytes in 1 blocks
            suppressed: 0 bytes in 0 blocks
> vg_replace_malloc.c:307

What do you think that is? Valgrind tracks allocations by providing other implementations for malloc/free/... .

replies(1): >>45961844 #
14. vkou ◴[] No.45955980{4}[source]
That depends on what your error recovery plan is.

If the code's running in a space shuttle, you probably want to test that path.

If it's bootstrapping a replicated service, it's likely desirable to crash early if a config file couldn't be opened.

If it's plausible that the file in question is missing, you can absolutely test that code path, without mocking open.

If you want to explicitly handle different reasons for why opening a file failed differently, by all means, stress all of that in your tests. But if all you have is a happy path and an unhappy path, where your code doesn't care why opening a file failed, all you need to test is the case where the file is present, and one where it is not.

replies(1): >>45959901 #
15. 1718627440 ◴[] No.45959901{5}[source]
Modifying the file system to be would be kind of like mocking to me. I very much, don't want my daemons or user-facing applications to just crash, when a file is missing. That's kind-of the worst thing you can do.
replies(1): >>45961597 #
16. vkou ◴[] No.45961597{6}[source]
> Modifying the file system to be would be kind of like mocking to me.

Modifying the file system's implementation would be. Including a valid_testdata.txt and an invalid_testdata.txt file in your test's directory, however, is not 'modifying the file system', any more than declaring a test input variable is 'mocking memory access'.

> don't want my daemons or user-facing applications to just crash, when a file is missing

If the file is important, it's the best kind of thing you can do when implementing a non-user-facing service. The last thing you want to do is to silently and incorrectly serve traffic because you are missing configuration.

You want to crash quickly and let whatever monitoring system you have in place escalate the problem in an application-agnostic manner.

17. paulf38 ◴[] No.45961844{4}[source]
Are you trying to explain to me how Valgrind works? If you do know more than me then please join us and become a Valgrind developer.

Mostly it wraps system calls and library calls. Wrapping means that it does some checking or recording before and maybe after the call. Very occasionally it needs to modify the arguments to the call. The rest of the time it passes the arguments on to the kernel or libc/libpthread/C++ lib.

There are also functions and syscalls that it needs to replace. That needs to be a fully functional replacement, not just looking the same as in mocking.

I don’t have any exact figures. The number of syscalls varies quite a lot by platform and on most platforms there are many obsolete syscalls that are not implemented. At a rough guess, I’d say there are something like 300 syscalls and 100 lib calls that are handled of which 3/4 are wrapped and 1/4 are replaced.

replies(1): >>45965100 #
18. 1718627440 ◴[] No.45965100{5}[source]
> Are you trying to explain to me how Valgrind works?

Sorry that wasn't my intention. You are a Valgrind developer? Thanks, it's a good project.

It seems like I have a different understanding of mocking than other people in the thread and it shows. My understanding was, that Valgrind provides function replacements via dynamic linking, that then call into the real libc. I would call that mocking, but YMMV.