this post was submitted on 19 Aug 2023
3 points (100.0% liked)

Learn Programming

155 readers
1 users here now

A place to learn programming.
Share free resources here.

Ask questions on heapoverflow.ml

Python docs (tutorial)
Javascript docs (node)
Hard challenges (Project Euler)

founded 2 years ago
MODERATORS
 

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.

top 3 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 year ago

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

[–] [email protected] 2 points 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.

[–] [email protected] 2 points 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.