0

Does any one know what type of attribute can we use to run all of the unit tests independently? for example in following we used [Fact] attribute but it does not make tests run independently. Is there any other type attribute that we can use to initialize data at beginning of each test and make tests run independently from each other? How can I run unit tests independently in visual studio code?

namespace Tests { public class TestCategory { //Test Get() Method [Fact] public void Get_WhenCalled_ReturnsAllCategories() { //Arrange //create _options var _Options = new DbContextOptionsBuilder<MyContext>() .UseInMemoryDatabase("Data Source=MyCart.db").Options; var Context = new MyContext(_Options); Context.CategoryTestData();//We make sure that dummy data has been added var Controller = new CategoryController(Context);//pass context inside controller //Act var Results = Controller.Get();//call Get() function inside Category controller //Assert Assert.NotNull(Results); } //Test GetById() Method //When valid Id is passed [Fact] public void GetById_ExistingIntIdPassed_ReturnsOkResult() { //Arrange var _Options = new DbContextOptionsBuilder<MyContext>() .UseInMemoryDatabase("Data Source=MyCart.db").Options; var Context = new MyContext(_Options);//pass _Options into context var Controller = new CategoryController(Context);//pass Context inside controller //Act var OkResult = Controller.GetById(1);//1 is valid Id //Assert Assert.IsType<OkObjectResult>(OkResult.Result); } } } 
4
  • 2
    What do you mean by "independently"? Commented Aug 29, 2020 at 16:46
  • Does this answer your question? Execute unit tests serially (rather than in parallel) Commented Aug 29, 2020 at 16:53
  • Yeh I already tried that but it did not work. Commented Aug 29, 2020 at 16:59
  • @MJZ there are multiple solutions in that question. What did you try and how did it not work? Commented Aug 29, 2020 at 17:05

3 Answers 3

0

If you are using the Visual Studio IDE you should try to check the Run Tests In Parallel on Test Explorer window. And then your tests should run in parallel. enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for the answer, I am using Visual Studio Code
0

If you add a constructor to your class you can set up "initialisation" data etc..

There is no [Attribute], if you know other test frameworks, you can have a look at a comparison list to see how the [Attributes] compares to xUnit.

When you look at *Note 3 on that page you will see it describes the use of an IClassFixture<T> interface that can be used to share context between tests

For parallel testing you can configure it in the configuration of xUnit.

Comments

0

I finally found the answer here: How to isolate EF InMemory database per XUnit test Actually we want that our context not to be shared between the tests hence we have to make a new context at the beginning of each test like this:

using (var Context = new myContext(CreateNewContextOptions())) { //here we implement Arrange, Act, and Assert parts } 

CreateNewContextOptions() is actually a function that helps us to create a new context.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.