You Can't Unit Test C, Right?

Поділитися
Вставка
  • Опубліковано 8 вер 2024

КОМЕНТАРІ • 37

  • @edgeeffect
    @edgeeffect 3 роки тому +21

    Benno Rice always seems to come up with a great talk! I'm beginning to think you could say "Benno, can you do us a talk about vacuuming your stairs?" and at the end of it we'd all say "that was really interesting and kinda funny"...

  • @nextlifeonearth
    @nextlifeonearth 2 роки тому +21

    Having experience writing unit tests for C I may also add: avoid globals like the plague it is and if you do have globals, make the fixture of the code that uses it reset the entire thing every single unit test.
    I've had far too often that if the testing order changes, half the unit tests suddenly fail because some unit test was dependent on some other unit test being run first.

    • @JKTCGMV13
      @JKTCGMV13 2 роки тому +2

      This is the challenge I’m facing in the legacy C codebase I’m trying to figure out how to test. Not only is the entire thing built around globals, but they are nearly all referenced by extern calls instead of included headers

    • @Seff2
      @Seff2 2 роки тому

      @@JKTCGMV13 does it make a difference that the variables are extern? Can't you still set them like normal variables?

    • @JKTCGMV13
      @JKTCGMV13 2 роки тому +1

      @@Seff2 as far as code functionality goes, technically it doesn’t matter since it compiles and runs. But it’s a significant negative impact on readability, maintainability, etc.
      I can also imagine a scenario where you could include one header with all the globals in production and swap info via header files for testing, which could be harder with externs.

    • @edgeeffect
      @edgeeffect 10 місяців тому

      Yeah...surely "avoid globals like the plague" is sound advice if you're testing or not.

    • @jmdavison62
      @jmdavison62 10 місяців тому +1

      It's amazing how many people ignore what you just posted, all of which ought to be common sense.
      Global variables (incl. "Singleton," "Monostate," etc.) are a necessary evil at best and should be avoided where possible, and contained otherwise. There are a handful of exceptions (a process-global logger, read-only data that cannot possibly change), but even then, one should use handles to the data wherever possible.

  • @tendaiwindhoek5982
    @tendaiwindhoek5982 5 років тому +20

    I like testing and I like C. Great presentation sir,.

  • @johanneswestman935
    @johanneswestman935 3 роки тому +30

    Very good presentation. I failed my first interview because they wanted me to do unit testing in C and I was claiming it couldn't really, truly, be done and that we had not tested that way in my previous job. To be fair my previous job was very close to hardware so our tests consisted of "does this LED blink when I want it to"?

    • @gregoryfenn1462
      @gregoryfenn1462 2 роки тому +9

      My job is low level embedded too but I promise you that we’d have stern words if you said you can’t do unit testing in C (or even assembly code!). I promise you can and it’s not even as hard as it sounds. The main thing is to have a database file of preconditions (states) and expected postconditions (Boolean functions on the states) for each function call. The function signatures can be declared in the unit tests files or via headers. There is no need to add unit testing logic into the source files for the functions to be tested - and indeed that would be very bad because it mixes a the purpose of one file into two tasks which is a nightmare to debug or analyse.

  • @Faithy2386
    @Faithy2386 2 роки тому +8

    first time I've seen Benno Rice actually transcribed correctly by auto-caption, and not "dinner rice"

  • @FarhanKhan-ji5yl
    @FarhanKhan-ji5yl 4 роки тому +5

    In production!

  • @electronicwoe
    @electronicwoe 6 років тому +9

    Very good presenter!

  • @matthewrease2376
    @matthewrease2376 2 роки тому +2

    Valgrind is my friend :)

  • @dzidmail
    @dzidmail 2 місяці тому

    I don't think i could test static functions with this.

  • @krumbergify
    @krumbergify 2 роки тому

    cmocka supports link time mocking, but it gets ugly when people write tests that directly mock the internal implemention. If you that then your tests won’t help you if you refactor during code because you don’t test the API as you should.

    • @edgeeffect
      @edgeeffect 10 місяців тому

      I've always considered needing mocks in a unit test to be, not necessarily a code smell, but at least a slight whiff.

  • @bbdgl7413
    @bbdgl7413 5 років тому +7

    void fizzbuzz(int n)
    {
    if(n%3==0) printf("fizz");
    if(n%5==0) printf("buzz");
    printf("
    ");
    }
    I guess thats why its a common interview question...

    • @jabuci
      @jabuci 4 роки тому +7

      That's not a function but a procedure. Thank you. Next!

  • @3v1Bunny
    @3v1Bunny 4 роки тому +20

    It is pretty awkward that people are so opposed to C :(

    • @edgeeffect
      @edgeeffect 3 роки тому +4

      I dunno... it fills me with some kind of feeling of (comedic megalomaniac voice) "ha ha ha... revenge is mine!" ... after all those times that C programmers have derided my Pascal, my Ada, my Assembly Language and, most of all, my JavaScript... it's "nice" to see the C programmers get some of those decades of derision back. ?????? ;) ?????????

    • @antilogism
      @antilogism 3 роки тому +1

      @@edgeeffect I love C but I would never deride an Ada programmer!

    • @goqsane
      @goqsane 9 місяців тому

      Well. It would help to do such amazing presentations wearing something clean and respectful of the audience.

  • @gabrielcoronelcascante9111
    @gabrielcoronelcascante9111 3 роки тому +3

    strdup is not part of the C Standard :)

    • @lineline8034
      @lineline8034 3 роки тому

      But it do have in Unix-like world. Anyway, read the manual pages

  • @axelvonderweth4614
    @axelvonderweth4614 3 роки тому

    Bei dieser Sprache braucht man eine Glückskatze

  • @Acid31337
    @Acid31337 4 роки тому +1

    What point of making unit tests for C?
    It's blazing fast, just cover it with integration/e2e tests, so you will benefit from its basic feature, not struggling with accidential complexity(which is also C's feature) with unit testing

    • @TiagoJoaoSilva
      @TiagoJoaoSilva 3 роки тому +6

      *remote code executes on you*

    • @brentgreeff1115
      @brentgreeff1115 3 роки тому

      I prefer e2e over unit test, this is the norm in Ruby/ Rails, but how do I e2e a game written in C?

    • @gregoryfenn1462
      @gregoryfenn1462 3 роки тому

      What’s an e2e test and how does it cover all the ins and outs of every function of a app?

    • @krumbergify
      @krumbergify 2 роки тому +5

      The happy path is easy and efficient to test using integration tests, but testing error handling or other edge cases is better done using unit tests.

    • @Acid31337
      @Acid31337 2 роки тому

      @@krumbergify
      Of course integration tests will not show exact place where it gone wrong.
      But
      1. You ll guess it by last diff which broke tests.
      2. Unit tests will not show exact error place whatsoever, they'll be green all the time.

  • @jmdavison62
    @jmdavison62 10 місяців тому

    There's a strawman title. Why couldn't you?