You Can't Unit Test C, Right?

Поделиться
HTML-код
  • Опубликовано: 10 сен 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.

  • @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 10 месяцев назад

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

  • @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.

  • @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

  • @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!

  • @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.

  • @axelvonderweth4614
    @axelvonderweth4614 3 года назад

    Bei dieser Sprache braucht man eine Glückskatze

  • @jmdavison62
    @jmdavison62 10 месяцев назад

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