Most introductions to unit testing give very simple examples of functions that simply receive some arguments and produce a result. However a lot of software has to read input from external sources, such as from the internet, from the file system, or from the user during its execution. How does one write tests for such software?

For simple software that only reads a few files from the file system, I imagine can be tested by writing some files for the test to give to the tested program, but what if it becomes more complex? Or what if you are trying to test a web scrapper for a website, would the tests run a web server and simulate the targetted website? Or for the GUI, how would one test that the user can see what they are supposed to see, when they click a certain way at a certain time?

Maybe the parts that read the data shouldn’t be tested and only the functions that code relies on should be tested? I don’t know.

  • solrize
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    Terms to look up: mocks, dependency injection, integration tests. At the end for a web app, you run a full headless browser under Puppeteer or Selenium to exercise all the site and UI functions. That is considered an end to end test rather than a unit test.