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.

  • zoostation@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    1 year ago

    There are things called mocks that simulate a filesystem or database or other external API/resource for the purpose of testing.

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

  • BaconIsAVeg
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    There’s unit tests like you’ve mentioned, but that’s only a part of the picture. What you’re describing is functional testing. Usually you want the backend unit testing, along with a framework to do API testing, and then frontend testing using browser automation (selenium, gauge, etc) depending on your needs.