Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: data driven testing #52

Open
xhd2015 opened this issue Apr 11, 2024 · 5 comments
Open

Discussion: data driven testing #52

xhd2015 opened this issue Apr 11, 2024 · 5 comments
Labels

Comments

@xhd2015
Copy link
Owner

xhd2015 commented Apr 11, 2024

Never depend on the implementation directly. Abstract a layer between the implementation and the intent.

For example, when testing a service with external dependencies, we want to mock all of them.

But we should not directly mock their request or response, instead, we abstract a data structure called TestingContext.

This TestingContext is declared as a universal data fake storage:

type TestingContext struct {
    Users []*User
    Checkouts []*Checkout
    Config *Config
}

Then each dependency read data from this central data storage, and construct corresponding response based on the data.

And to reduce duplication of repeating declaring TestingContext, different tests can share a basic TestingContext, adding their custom overlay to the context.

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 11, 2024

Two important concepts:

  • fake data storage
  • data overlay

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 11, 2024

Data driven requires the user to have a basic knowledge of the system's models as well as APIs to interact with these models.

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 11, 2024

But data drive testing is already taken, also known as table driven testing. Wiki: https://en.m.wikipedia.org/wiki/Data-driven_testing.

However, given that table driven is widely used and has gained a relative stable shape(a table), then this method can still be referred as Data driven testing.

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 11, 2024

Data driven requires the user to have a basic knowledge of the system's models as well as APIs to interact with these models.

To do data driven testing, following these 4 steps:

  • recognize entities in the system,
  • define their ids and properties ,
  • mock the API behavior based on the mocking data
    • this can be done by comparing the querying key with the data context, and retrieve matching result from the context. panic out if no data found
  • construct base data context, and create overlays to repeat the test for different logic paths

(draw a diagram to illustrate the relation of data and APIs, APIs are fall into 2 categories: internal and external like database.)

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 11, 2024

Data driven requires the user to have a basic knowledge of the system's models as well as APIs to interact with these models.

To do data driven testing, following these 4 steps:

  • recognize entities in the system,

  • define their ids and properties ,

  • mock the API behavior based on the mocking data

    • this can be done by comparing the querying key with the data context, and retrieve matching result from the context. panic out if no data found
  • construct base data context, and create overlays to repeat the test for different logic paths

(draw a diagram to illustrate the relation of data and APIs, APIs are fall into 2 categories: internal and external like database.)

The reverse process, convert real API response to data context, is also valuable. Compared to that directly replay real traffic, we actually split that into 2 phase:

  • extract
  • replay with mock

With this, unit test can be more easily constructed and maintained from live traffic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant