Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:22:42+00:00 2026-06-02T23:22:42+00:00

Lets say you have a website, that uses a function to retrieve data from

  • 0

Lets say you have a website, that uses a function to retrieve data from the database and returns the result to be displayed/parsed/etc…

Since the data that is retrieved from the database is dynamic and could potentially change every second of the day, how do you properly write a Unit Test for this function?

Let’s say the function is supposed to return an array of results. Obviously a unit test could test to see if an array is returned or not. But what happens when the contents of the array itself are incorrect due to an incorrectly written MySQL query? The size of the array could be zero or the content of the array could be incorrect. Since it relies on ever-changing data, how would the Unit Test know what is correct and what isn’t? Would calls to the database from within the Unit Test itself be necessary so there is something to compare it to?

How do you properly write a Unit Test for functions that rely on dynamic data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:22:45+00:00Added an answer on June 2, 2026 at 11:22 pm

    Unit tests, in their ideal form, should only test one thing. In this case, you’re testing two things:

    1. the logic of your function
    2. the database retrieval

    So I would suggest the following refactor:

    1. Move the database retrieval logic into a separate function
    2. Have the function you want to test call that other function
    3. Mock out the function that returns data so you can unit test just the logic of your app
    4. If it makes sense (if you’re just relying on another library to do this, then hopefully that lib already has tests), write a unit test for the dynamic retrieval function, where you can’t test specifics, but can can test the structure and reasonableness of the data returned (e.g. it has all the fields set, and is a time within 5 seconds of now).

    Also, typically it’s a good idea to run unit tests in a test environment where you have complete control over what’s stored in the database. You don’t want to run these against production data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i have a website mysite.com that will store some sensitive personal data
I have a form that is generated on a customer's website (lets say customer.com)
Lets say I have a website that sells widgets. I would like to do
Lets say I have a website with a small application that lists the 10
Let's say you have a website that lets you build an account with up
Lets say you have website that keeps balance for each user. You give each
Lets say you have a large, popular database-driven website. There are people on the
lets say i have a website that i am developing... the site may have
I have 2 websites, lets say - example.com and example1.com example.com has a database
This is my first question on this wonderful website. Lets say I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.