Creating coded data objects using real data from database for unit testing





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















I'm going to write some unit tests for my method:



using System.Linq;
...
public int CountEnabledEvents(IEnumerable<Event> events)
{
return events.Count(e => e.IsEnabled);
}


As you can see the input of this method is a IEnumerable<Event> collection.



The Event class looks like



public class Event
{
public Guid Id { get;set; }
public bool IsEnabled { get;set; }
public string SomeOtherProp { get;set; }
...
}


So I was about to create some dummy events on my test class:



var event1 = new Event() {
Id = new Guid("..."),
IsEnabled = true
}
var event2 = new Event() {
Id = new Guid("xxx"),
IsEnabled = false
}


The thing is A) this is very tedious, and B) I actually have a ton of real events in my database, and I would like to use the real events on my unit test as testing with real data will make our test more accurate.



¿Is there any kind of software or method to create those dummy Event objects out of real data I have in the database in a automagical way?



Thank you for your time.



EDIT



I thought it was obvius that the code I've provided is a simplification of the actual code. The real method I would like to Unit-test is more complex, as is the actual Event.



So yes, its quite tedious to write down the N^N possibilities for the real event object. And no, I'm not trying to test the integration with the database or anything like that. What I'm trying to do is create static instances coded in the actual test with the data I've got in the database. Not at the testing run-time, but earlier.










share|improve this question

























  • The code I used in the question is an oversimplification

    – r1verside
    Nov 16 '18 at 17:25











  • Check out Bogus for creating fake data. It is all in memory. Not a Db connection tool.

    – Nkosi
    Nov 20 '18 at 1:16













  • Thank you @Nkosi I've already been there, the thing is that Bogus creates fake data while I'm looking to work with real data.

    – r1verside
    Nov 21 '18 at 15:52











  • Could downvoters please explain their reasons?

    – r1verside
    Nov 21 '18 at 15:53


















-2















I'm going to write some unit tests for my method:



using System.Linq;
...
public int CountEnabledEvents(IEnumerable<Event> events)
{
return events.Count(e => e.IsEnabled);
}


As you can see the input of this method is a IEnumerable<Event> collection.



The Event class looks like



public class Event
{
public Guid Id { get;set; }
public bool IsEnabled { get;set; }
public string SomeOtherProp { get;set; }
...
}


So I was about to create some dummy events on my test class:



var event1 = new Event() {
Id = new Guid("..."),
IsEnabled = true
}
var event2 = new Event() {
Id = new Guid("xxx"),
IsEnabled = false
}


The thing is A) this is very tedious, and B) I actually have a ton of real events in my database, and I would like to use the real events on my unit test as testing with real data will make our test more accurate.



¿Is there any kind of software or method to create those dummy Event objects out of real data I have in the database in a automagical way?



Thank you for your time.



EDIT



I thought it was obvius that the code I've provided is a simplification of the actual code. The real method I would like to Unit-test is more complex, as is the actual Event.



So yes, its quite tedious to write down the N^N possibilities for the real event object. And no, I'm not trying to test the integration with the database or anything like that. What I'm trying to do is create static instances coded in the actual test with the data I've got in the database. Not at the testing run-time, but earlier.










share|improve this question

























  • The code I used in the question is an oversimplification

    – r1verside
    Nov 16 '18 at 17:25











  • Check out Bogus for creating fake data. It is all in memory. Not a Db connection tool.

    – Nkosi
    Nov 20 '18 at 1:16













  • Thank you @Nkosi I've already been there, the thing is that Bogus creates fake data while I'm looking to work with real data.

    – r1verside
    Nov 21 '18 at 15:52











  • Could downvoters please explain their reasons?

    – r1verside
    Nov 21 '18 at 15:53














-2












-2








-2


1






I'm going to write some unit tests for my method:



using System.Linq;
...
public int CountEnabledEvents(IEnumerable<Event> events)
{
return events.Count(e => e.IsEnabled);
}


As you can see the input of this method is a IEnumerable<Event> collection.



The Event class looks like



public class Event
{
public Guid Id { get;set; }
public bool IsEnabled { get;set; }
public string SomeOtherProp { get;set; }
...
}


So I was about to create some dummy events on my test class:



var event1 = new Event() {
Id = new Guid("..."),
IsEnabled = true
}
var event2 = new Event() {
Id = new Guid("xxx"),
IsEnabled = false
}


The thing is A) this is very tedious, and B) I actually have a ton of real events in my database, and I would like to use the real events on my unit test as testing with real data will make our test more accurate.



¿Is there any kind of software or method to create those dummy Event objects out of real data I have in the database in a automagical way?



Thank you for your time.



EDIT



I thought it was obvius that the code I've provided is a simplification of the actual code. The real method I would like to Unit-test is more complex, as is the actual Event.



So yes, its quite tedious to write down the N^N possibilities for the real event object. And no, I'm not trying to test the integration with the database or anything like that. What I'm trying to do is create static instances coded in the actual test with the data I've got in the database. Not at the testing run-time, but earlier.










share|improve this question
















I'm going to write some unit tests for my method:



using System.Linq;
...
public int CountEnabledEvents(IEnumerable<Event> events)
{
return events.Count(e => e.IsEnabled);
}


As you can see the input of this method is a IEnumerable<Event> collection.



The Event class looks like



public class Event
{
public Guid Id { get;set; }
public bool IsEnabled { get;set; }
public string SomeOtherProp { get;set; }
...
}


So I was about to create some dummy events on my test class:



var event1 = new Event() {
Id = new Guid("..."),
IsEnabled = true
}
var event2 = new Event() {
Id = new Guid("xxx"),
IsEnabled = false
}


The thing is A) this is very tedious, and B) I actually have a ton of real events in my database, and I would like to use the real events on my unit test as testing with real data will make our test more accurate.



¿Is there any kind of software or method to create those dummy Event objects out of real data I have in the database in a automagical way?



Thank you for your time.



EDIT



I thought it was obvius that the code I've provided is a simplification of the actual code. The real method I would like to Unit-test is more complex, as is the actual Event.



So yes, its quite tedious to write down the N^N possibilities for the real event object. And no, I'm not trying to test the integration with the database or anything like that. What I'm trying to do is create static instances coded in the actual test with the data I've got in the database. Not at the testing run-time, but earlier.







c# unit-testing domain-driven-design






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 17:33







r1verside

















asked Nov 16 '18 at 16:43









r1versider1verside

1,157827




1,157827













  • The code I used in the question is an oversimplification

    – r1verside
    Nov 16 '18 at 17:25











  • Check out Bogus for creating fake data. It is all in memory. Not a Db connection tool.

    – Nkosi
    Nov 20 '18 at 1:16













  • Thank you @Nkosi I've already been there, the thing is that Bogus creates fake data while I'm looking to work with real data.

    – r1verside
    Nov 21 '18 at 15:52











  • Could downvoters please explain their reasons?

    – r1verside
    Nov 21 '18 at 15:53



















  • The code I used in the question is an oversimplification

    – r1verside
    Nov 16 '18 at 17:25











  • Check out Bogus for creating fake data. It is all in memory. Not a Db connection tool.

    – Nkosi
    Nov 20 '18 at 1:16













  • Thank you @Nkosi I've already been there, the thing is that Bogus creates fake data while I'm looking to work with real data.

    – r1verside
    Nov 21 '18 at 15:52











  • Could downvoters please explain their reasons?

    – r1verside
    Nov 21 '18 at 15:53

















The code I used in the question is an oversimplification

– r1verside
Nov 16 '18 at 17:25





The code I used in the question is an oversimplification

– r1verside
Nov 16 '18 at 17:25













Check out Bogus for creating fake data. It is all in memory. Not a Db connection tool.

– Nkosi
Nov 20 '18 at 1:16







Check out Bogus for creating fake data. It is all in memory. Not a Db connection tool.

– Nkosi
Nov 20 '18 at 1:16















Thank you @Nkosi I've already been there, the thing is that Bogus creates fake data while I'm looking to work with real data.

– r1verside
Nov 21 '18 at 15:52





Thank you @Nkosi I've already been there, the thing is that Bogus creates fake data while I'm looking to work with real data.

– r1verside
Nov 21 '18 at 15:52













Could downvoters please explain their reasons?

– r1verside
Nov 21 '18 at 15:53





Could downvoters please explain their reasons?

– r1verside
Nov 21 '18 at 15:53












3 Answers
3






active

oldest

votes


















2














I really dont get what's tedious or why using real data would make the test more accurate. You are only looking at the IsEnabled property anyway. A few hard-coded events would do it, but if you want just generated randomized events where the unit test setup tracks how many IsEnabled = true were generated and compare that with the result of calling CountEnabledEvents(randomizedEvents).




The code I shown is just a simplification of the actual one, where
there are lot of possible combinations.




If you want real data as a static unit testing sample there's 2 things that comes to my mind:




  1. Create a C# program that reads events from the DB and generates the C# code to instantiate these events, which you then copy in your unit testing setup.


  2. You probably already have a mechanism for serializing/de-serializing your events (e.g. from/to JSON). You could read events from the DB in their serialized form, copy that in your unit testing setup, which will also de-serialize the events at runtime.







share|improve this answer


























  • The code I shown is just a simplification of the actual one, where there are lot of possible combinations. And random is not an option since, there're properties that are strings but there's a set of possible strings, and also possible combinations of that string with other properties, so that's why I'm asking how to automatically hard-code those out of the actual database data.

    – r1verside
    Nov 16 '18 at 17:24













  • Yep, I was about to take option 1. That's why I came here to ask if isn't there anyone that faced this very situation and created an application of some sort to do this.

    – r1verside
    Nov 16 '18 at 18:01






  • 1





    @r1verside I doubt it. It's a very specific use case. You would probably be better off with option 2 however, since you have to have these mechanisms already in your system to store & retrieve events. It's just a matter of hard-coding the data storage form of your events and then use the de-serializer at runtime. That should be pretty straight forward to implement.

    – plalx
    Nov 16 '18 at 18:37













  • Okay, seems you were right and there's no tool atm that could help me achieve my goals atm. Thank you.

    – r1verside
    Nov 21 '18 at 15:55



















1





+100









There are probably a couple of ways to approach this:



You seem to be after state-based unit tests but the test data is extensively represented in a data store. You could use that data store to churn out some state (objects) in c# by generating the relevant "data sets" and then use that.



Another option may be to ring-fence the data in a test data store that serves as the relevant data set and then have some factory/repository/DAO produce the required objects directly from the data store. The data set could be extracted into a much smaller data base that is saved somewhere that is accessible or it may even be extracted into some other format (xml, json) and retrieved from there.



I'm note aware of any mapping software that is going to produce data from a database but I'm thinking that it is probably not too much effort to just write something yourself as this is probably a rather specific bit of data that you are referring to.



I really don't see anything wrong with obtaining the test data from a database for state-based testing; especially if it covers a decent number of possibilities.



You may event go as far as creating an attribute that injects the test values but retrieves them via some implementation of an interface. Along the lines of the TestCase Attribute. It will be slightly more involved than the TestCase attribute and may be a specific attributes that know how to retrieve the data.



update:



I just read the answer provided by p|a|x and we seem to be thinking along the same lines :)






share|improve this answer
























  • Got your book on my desk btw ;) Haven't had the time to go through it yet. Already read the blue & red books though and planning on going through Greg's Versioning one soon as well.

    – plalx
    Nov 20 '18 at 22:01













  • Okey thank you both, I've given you the rep bounty because I think it's a little more explainatory than @plalx but I give him credit too by marking his answer as accepted. Thanks!

    – r1verside
    Nov 21 '18 at 15:50













  • Thanks for the bounty @r1verside and thanks for getting my book @p|a|x :) --- please e-mail me your thoughts on the content once you've gotten around to taking a look...

    – Eben Roux
    Nov 23 '18 at 4:03



















-1














I think you are on the wrong way. Maybe you misunderstood the meaning of a unit test.




  • A unit test is just for verifing your code still have the same behaviour after changes.


You will need to search for integration testing for your purposes. If you would like to use it with entity framework 6. Maybe you should have a look to this : https://github.com/zzzprojects/EntityFramework-Effort ( be aware they are also mixing unit and integration tests)






share|improve this answer
























  • I'm afraid I'm not. What I'm asking is not related to integration tests.

    – r1verside
    Nov 16 '18 at 17:32












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342087%2fcreating-coded-data-objects-using-real-data-from-database-for-unit-testing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I really dont get what's tedious or why using real data would make the test more accurate. You are only looking at the IsEnabled property anyway. A few hard-coded events would do it, but if you want just generated randomized events where the unit test setup tracks how many IsEnabled = true were generated and compare that with the result of calling CountEnabledEvents(randomizedEvents).




The code I shown is just a simplification of the actual one, where
there are lot of possible combinations.




If you want real data as a static unit testing sample there's 2 things that comes to my mind:




  1. Create a C# program that reads events from the DB and generates the C# code to instantiate these events, which you then copy in your unit testing setup.


  2. You probably already have a mechanism for serializing/de-serializing your events (e.g. from/to JSON). You could read events from the DB in their serialized form, copy that in your unit testing setup, which will also de-serialize the events at runtime.







share|improve this answer


























  • The code I shown is just a simplification of the actual one, where there are lot of possible combinations. And random is not an option since, there're properties that are strings but there's a set of possible strings, and also possible combinations of that string with other properties, so that's why I'm asking how to automatically hard-code those out of the actual database data.

    – r1verside
    Nov 16 '18 at 17:24













  • Yep, I was about to take option 1. That's why I came here to ask if isn't there anyone that faced this very situation and created an application of some sort to do this.

    – r1verside
    Nov 16 '18 at 18:01






  • 1





    @r1verside I doubt it. It's a very specific use case. You would probably be better off with option 2 however, since you have to have these mechanisms already in your system to store & retrieve events. It's just a matter of hard-coding the data storage form of your events and then use the de-serializer at runtime. That should be pretty straight forward to implement.

    – plalx
    Nov 16 '18 at 18:37













  • Okay, seems you were right and there's no tool atm that could help me achieve my goals atm. Thank you.

    – r1verside
    Nov 21 '18 at 15:55
















2














I really dont get what's tedious or why using real data would make the test more accurate. You are only looking at the IsEnabled property anyway. A few hard-coded events would do it, but if you want just generated randomized events where the unit test setup tracks how many IsEnabled = true were generated and compare that with the result of calling CountEnabledEvents(randomizedEvents).




The code I shown is just a simplification of the actual one, where
there are lot of possible combinations.




If you want real data as a static unit testing sample there's 2 things that comes to my mind:




  1. Create a C# program that reads events from the DB and generates the C# code to instantiate these events, which you then copy in your unit testing setup.


  2. You probably already have a mechanism for serializing/de-serializing your events (e.g. from/to JSON). You could read events from the DB in their serialized form, copy that in your unit testing setup, which will also de-serialize the events at runtime.







share|improve this answer


























  • The code I shown is just a simplification of the actual one, where there are lot of possible combinations. And random is not an option since, there're properties that are strings but there's a set of possible strings, and also possible combinations of that string with other properties, so that's why I'm asking how to automatically hard-code those out of the actual database data.

    – r1verside
    Nov 16 '18 at 17:24













  • Yep, I was about to take option 1. That's why I came here to ask if isn't there anyone that faced this very situation and created an application of some sort to do this.

    – r1verside
    Nov 16 '18 at 18:01






  • 1





    @r1verside I doubt it. It's a very specific use case. You would probably be better off with option 2 however, since you have to have these mechanisms already in your system to store & retrieve events. It's just a matter of hard-coding the data storage form of your events and then use the de-serializer at runtime. That should be pretty straight forward to implement.

    – plalx
    Nov 16 '18 at 18:37













  • Okay, seems you were right and there's no tool atm that could help me achieve my goals atm. Thank you.

    – r1verside
    Nov 21 '18 at 15:55














2












2








2







I really dont get what's tedious or why using real data would make the test more accurate. You are only looking at the IsEnabled property anyway. A few hard-coded events would do it, but if you want just generated randomized events where the unit test setup tracks how many IsEnabled = true were generated and compare that with the result of calling CountEnabledEvents(randomizedEvents).




The code I shown is just a simplification of the actual one, where
there are lot of possible combinations.




If you want real data as a static unit testing sample there's 2 things that comes to my mind:




  1. Create a C# program that reads events from the DB and generates the C# code to instantiate these events, which you then copy in your unit testing setup.


  2. You probably already have a mechanism for serializing/de-serializing your events (e.g. from/to JSON). You could read events from the DB in their serialized form, copy that in your unit testing setup, which will also de-serialize the events at runtime.







share|improve this answer















I really dont get what's tedious or why using real data would make the test more accurate. You are only looking at the IsEnabled property anyway. A few hard-coded events would do it, but if you want just generated randomized events where the unit test setup tracks how many IsEnabled = true were generated and compare that with the result of calling CountEnabledEvents(randomizedEvents).




The code I shown is just a simplification of the actual one, where
there are lot of possible combinations.




If you want real data as a static unit testing sample there's 2 things that comes to my mind:




  1. Create a C# program that reads events from the DB and generates the C# code to instantiate these events, which you then copy in your unit testing setup.


  2. You probably already have a mechanism for serializing/de-serializing your events (e.g. from/to JSON). You could read events from the DB in their serialized form, copy that in your unit testing setup, which will also de-serialize the events at runtime.








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 17:44

























answered Nov 16 '18 at 17:12









plalxplalx

33.4k44870




33.4k44870













  • The code I shown is just a simplification of the actual one, where there are lot of possible combinations. And random is not an option since, there're properties that are strings but there's a set of possible strings, and also possible combinations of that string with other properties, so that's why I'm asking how to automatically hard-code those out of the actual database data.

    – r1verside
    Nov 16 '18 at 17:24













  • Yep, I was about to take option 1. That's why I came here to ask if isn't there anyone that faced this very situation and created an application of some sort to do this.

    – r1verside
    Nov 16 '18 at 18:01






  • 1





    @r1verside I doubt it. It's a very specific use case. You would probably be better off with option 2 however, since you have to have these mechanisms already in your system to store & retrieve events. It's just a matter of hard-coding the data storage form of your events and then use the de-serializer at runtime. That should be pretty straight forward to implement.

    – plalx
    Nov 16 '18 at 18:37













  • Okay, seems you were right and there's no tool atm that could help me achieve my goals atm. Thank you.

    – r1verside
    Nov 21 '18 at 15:55



















  • The code I shown is just a simplification of the actual one, where there are lot of possible combinations. And random is not an option since, there're properties that are strings but there's a set of possible strings, and also possible combinations of that string with other properties, so that's why I'm asking how to automatically hard-code those out of the actual database data.

    – r1verside
    Nov 16 '18 at 17:24













  • Yep, I was about to take option 1. That's why I came here to ask if isn't there anyone that faced this very situation and created an application of some sort to do this.

    – r1verside
    Nov 16 '18 at 18:01






  • 1





    @r1verside I doubt it. It's a very specific use case. You would probably be better off with option 2 however, since you have to have these mechanisms already in your system to store & retrieve events. It's just a matter of hard-coding the data storage form of your events and then use the de-serializer at runtime. That should be pretty straight forward to implement.

    – plalx
    Nov 16 '18 at 18:37













  • Okay, seems you were right and there's no tool atm that could help me achieve my goals atm. Thank you.

    – r1verside
    Nov 21 '18 at 15:55

















The code I shown is just a simplification of the actual one, where there are lot of possible combinations. And random is not an option since, there're properties that are strings but there's a set of possible strings, and also possible combinations of that string with other properties, so that's why I'm asking how to automatically hard-code those out of the actual database data.

– r1verside
Nov 16 '18 at 17:24







The code I shown is just a simplification of the actual one, where there are lot of possible combinations. And random is not an option since, there're properties that are strings but there's a set of possible strings, and also possible combinations of that string with other properties, so that's why I'm asking how to automatically hard-code those out of the actual database data.

– r1verside
Nov 16 '18 at 17:24















Yep, I was about to take option 1. That's why I came here to ask if isn't there anyone that faced this very situation and created an application of some sort to do this.

– r1verside
Nov 16 '18 at 18:01





Yep, I was about to take option 1. That's why I came here to ask if isn't there anyone that faced this very situation and created an application of some sort to do this.

– r1verside
Nov 16 '18 at 18:01




1




1





@r1verside I doubt it. It's a very specific use case. You would probably be better off with option 2 however, since you have to have these mechanisms already in your system to store & retrieve events. It's just a matter of hard-coding the data storage form of your events and then use the de-serializer at runtime. That should be pretty straight forward to implement.

– plalx
Nov 16 '18 at 18:37







@r1verside I doubt it. It's a very specific use case. You would probably be better off with option 2 however, since you have to have these mechanisms already in your system to store & retrieve events. It's just a matter of hard-coding the data storage form of your events and then use the de-serializer at runtime. That should be pretty straight forward to implement.

– plalx
Nov 16 '18 at 18:37















Okay, seems you were right and there's no tool atm that could help me achieve my goals atm. Thank you.

– r1verside
Nov 21 '18 at 15:55





Okay, seems you were right and there's no tool atm that could help me achieve my goals atm. Thank you.

– r1verside
Nov 21 '18 at 15:55













1





+100









There are probably a couple of ways to approach this:



You seem to be after state-based unit tests but the test data is extensively represented in a data store. You could use that data store to churn out some state (objects) in c# by generating the relevant "data sets" and then use that.



Another option may be to ring-fence the data in a test data store that serves as the relevant data set and then have some factory/repository/DAO produce the required objects directly from the data store. The data set could be extracted into a much smaller data base that is saved somewhere that is accessible or it may even be extracted into some other format (xml, json) and retrieved from there.



I'm note aware of any mapping software that is going to produce data from a database but I'm thinking that it is probably not too much effort to just write something yourself as this is probably a rather specific bit of data that you are referring to.



I really don't see anything wrong with obtaining the test data from a database for state-based testing; especially if it covers a decent number of possibilities.



You may event go as far as creating an attribute that injects the test values but retrieves them via some implementation of an interface. Along the lines of the TestCase Attribute. It will be slightly more involved than the TestCase attribute and may be a specific attributes that know how to retrieve the data.



update:



I just read the answer provided by p|a|x and we seem to be thinking along the same lines :)






share|improve this answer
























  • Got your book on my desk btw ;) Haven't had the time to go through it yet. Already read the blue & red books though and planning on going through Greg's Versioning one soon as well.

    – plalx
    Nov 20 '18 at 22:01













  • Okey thank you both, I've given you the rep bounty because I think it's a little more explainatory than @plalx but I give him credit too by marking his answer as accepted. Thanks!

    – r1verside
    Nov 21 '18 at 15:50













  • Thanks for the bounty @r1verside and thanks for getting my book @p|a|x :) --- please e-mail me your thoughts on the content once you've gotten around to taking a look...

    – Eben Roux
    Nov 23 '18 at 4:03
















1





+100









There are probably a couple of ways to approach this:



You seem to be after state-based unit tests but the test data is extensively represented in a data store. You could use that data store to churn out some state (objects) in c# by generating the relevant "data sets" and then use that.



Another option may be to ring-fence the data in a test data store that serves as the relevant data set and then have some factory/repository/DAO produce the required objects directly from the data store. The data set could be extracted into a much smaller data base that is saved somewhere that is accessible or it may even be extracted into some other format (xml, json) and retrieved from there.



I'm note aware of any mapping software that is going to produce data from a database but I'm thinking that it is probably not too much effort to just write something yourself as this is probably a rather specific bit of data that you are referring to.



I really don't see anything wrong with obtaining the test data from a database for state-based testing; especially if it covers a decent number of possibilities.



You may event go as far as creating an attribute that injects the test values but retrieves them via some implementation of an interface. Along the lines of the TestCase Attribute. It will be slightly more involved than the TestCase attribute and may be a specific attributes that know how to retrieve the data.



update:



I just read the answer provided by p|a|x and we seem to be thinking along the same lines :)






share|improve this answer
























  • Got your book on my desk btw ;) Haven't had the time to go through it yet. Already read the blue & red books though and planning on going through Greg's Versioning one soon as well.

    – plalx
    Nov 20 '18 at 22:01













  • Okey thank you both, I've given you the rep bounty because I think it's a little more explainatory than @plalx but I give him credit too by marking his answer as accepted. Thanks!

    – r1verside
    Nov 21 '18 at 15:50













  • Thanks for the bounty @r1verside and thanks for getting my book @p|a|x :) --- please e-mail me your thoughts on the content once you've gotten around to taking a look...

    – Eben Roux
    Nov 23 '18 at 4:03














1





+100







1





+100



1




+100





There are probably a couple of ways to approach this:



You seem to be after state-based unit tests but the test data is extensively represented in a data store. You could use that data store to churn out some state (objects) in c# by generating the relevant "data sets" and then use that.



Another option may be to ring-fence the data in a test data store that serves as the relevant data set and then have some factory/repository/DAO produce the required objects directly from the data store. The data set could be extracted into a much smaller data base that is saved somewhere that is accessible or it may even be extracted into some other format (xml, json) and retrieved from there.



I'm note aware of any mapping software that is going to produce data from a database but I'm thinking that it is probably not too much effort to just write something yourself as this is probably a rather specific bit of data that you are referring to.



I really don't see anything wrong with obtaining the test data from a database for state-based testing; especially if it covers a decent number of possibilities.



You may event go as far as creating an attribute that injects the test values but retrieves them via some implementation of an interface. Along the lines of the TestCase Attribute. It will be slightly more involved than the TestCase attribute and may be a specific attributes that know how to retrieve the data.



update:



I just read the answer provided by p|a|x and we seem to be thinking along the same lines :)






share|improve this answer













There are probably a couple of ways to approach this:



You seem to be after state-based unit tests but the test data is extensively represented in a data store. You could use that data store to churn out some state (objects) in c# by generating the relevant "data sets" and then use that.



Another option may be to ring-fence the data in a test data store that serves as the relevant data set and then have some factory/repository/DAO produce the required objects directly from the data store. The data set could be extracted into a much smaller data base that is saved somewhere that is accessible or it may even be extracted into some other format (xml, json) and retrieved from there.



I'm note aware of any mapping software that is going to produce data from a database but I'm thinking that it is probably not too much effort to just write something yourself as this is probably a rather specific bit of data that you are referring to.



I really don't see anything wrong with obtaining the test data from a database for state-based testing; especially if it covers a decent number of possibilities.



You may event go as far as creating an attribute that injects the test values but retrieves them via some implementation of an interface. Along the lines of the TestCase Attribute. It will be slightly more involved than the TestCase attribute and may be a specific attributes that know how to retrieve the data.



update:



I just read the answer provided by p|a|x and we seem to be thinking along the same lines :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 6:22









Eben RouxEben Roux

9,73921538




9,73921538













  • Got your book on my desk btw ;) Haven't had the time to go through it yet. Already read the blue & red books though and planning on going through Greg's Versioning one soon as well.

    – plalx
    Nov 20 '18 at 22:01













  • Okey thank you both, I've given you the rep bounty because I think it's a little more explainatory than @plalx but I give him credit too by marking his answer as accepted. Thanks!

    – r1verside
    Nov 21 '18 at 15:50













  • Thanks for the bounty @r1verside and thanks for getting my book @p|a|x :) --- please e-mail me your thoughts on the content once you've gotten around to taking a look...

    – Eben Roux
    Nov 23 '18 at 4:03



















  • Got your book on my desk btw ;) Haven't had the time to go through it yet. Already read the blue & red books though and planning on going through Greg's Versioning one soon as well.

    – plalx
    Nov 20 '18 at 22:01













  • Okey thank you both, I've given you the rep bounty because I think it's a little more explainatory than @plalx but I give him credit too by marking his answer as accepted. Thanks!

    – r1verside
    Nov 21 '18 at 15:50













  • Thanks for the bounty @r1verside and thanks for getting my book @p|a|x :) --- please e-mail me your thoughts on the content once you've gotten around to taking a look...

    – Eben Roux
    Nov 23 '18 at 4:03

















Got your book on my desk btw ;) Haven't had the time to go through it yet. Already read the blue & red books though and planning on going through Greg's Versioning one soon as well.

– plalx
Nov 20 '18 at 22:01







Got your book on my desk btw ;) Haven't had the time to go through it yet. Already read the blue & red books though and planning on going through Greg's Versioning one soon as well.

– plalx
Nov 20 '18 at 22:01















Okey thank you both, I've given you the rep bounty because I think it's a little more explainatory than @plalx but I give him credit too by marking his answer as accepted. Thanks!

– r1verside
Nov 21 '18 at 15:50







Okey thank you both, I've given you the rep bounty because I think it's a little more explainatory than @plalx but I give him credit too by marking his answer as accepted. Thanks!

– r1verside
Nov 21 '18 at 15:50















Thanks for the bounty @r1verside and thanks for getting my book @p|a|x :) --- please e-mail me your thoughts on the content once you've gotten around to taking a look...

– Eben Roux
Nov 23 '18 at 4:03





Thanks for the bounty @r1verside and thanks for getting my book @p|a|x :) --- please e-mail me your thoughts on the content once you've gotten around to taking a look...

– Eben Roux
Nov 23 '18 at 4:03











-1














I think you are on the wrong way. Maybe you misunderstood the meaning of a unit test.




  • A unit test is just for verifing your code still have the same behaviour after changes.


You will need to search for integration testing for your purposes. If you would like to use it with entity framework 6. Maybe you should have a look to this : https://github.com/zzzprojects/EntityFramework-Effort ( be aware they are also mixing unit and integration tests)






share|improve this answer
























  • I'm afraid I'm not. What I'm asking is not related to integration tests.

    – r1verside
    Nov 16 '18 at 17:32
















-1














I think you are on the wrong way. Maybe you misunderstood the meaning of a unit test.




  • A unit test is just for verifing your code still have the same behaviour after changes.


You will need to search for integration testing for your purposes. If you would like to use it with entity framework 6. Maybe you should have a look to this : https://github.com/zzzprojects/EntityFramework-Effort ( be aware they are also mixing unit and integration tests)






share|improve this answer
























  • I'm afraid I'm not. What I'm asking is not related to integration tests.

    – r1verside
    Nov 16 '18 at 17:32














-1












-1








-1







I think you are on the wrong way. Maybe you misunderstood the meaning of a unit test.




  • A unit test is just for verifing your code still have the same behaviour after changes.


You will need to search for integration testing for your purposes. If you would like to use it with entity framework 6. Maybe you should have a look to this : https://github.com/zzzprojects/EntityFramework-Effort ( be aware they are also mixing unit and integration tests)






share|improve this answer













I think you are on the wrong way. Maybe you misunderstood the meaning of a unit test.




  • A unit test is just for verifing your code still have the same behaviour after changes.


You will need to search for integration testing for your purposes. If you would like to use it with entity framework 6. Maybe you should have a look to this : https://github.com/zzzprojects/EntityFramework-Effort ( be aware they are also mixing unit and integration tests)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 17:05









RichyP7RichyP7

315




315













  • I'm afraid I'm not. What I'm asking is not related to integration tests.

    – r1verside
    Nov 16 '18 at 17:32



















  • I'm afraid I'm not. What I'm asking is not related to integration tests.

    – r1verside
    Nov 16 '18 at 17:32

















I'm afraid I'm not. What I'm asking is not related to integration tests.

– r1verside
Nov 16 '18 at 17:32





I'm afraid I'm not. What I'm asking is not related to integration tests.

– r1verside
Nov 16 '18 at 17:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342087%2fcreating-coded-data-objects-using-real-data-from-database-for-unit-testing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python