vendredi 23 octobre 2020

Get type/name/properties of calling class when using DatabaseFixture xUnit

I am using xUnit to setup some integration tests and my setup is the following:

public class MyTest : IClassFixture<DatabaseFixture>
{
    private static readonly string _tableName = $"dbo.{TableNameConstants.MyTable}";

    public My_Test()
    {
        //do stuff
    }
}



public class DatabaseFixture : IDisposable
{
    public DatabaseFixture()
    {
        var aux = this.GetType().GetField("_tableName");
    }

    public void Dispose()
    {
        // ... clean up test data from the database ...
    }

}

What I would like to do is that at the beginning of every test I do some database cleanup. Meaning that I would like to get the name of the table the test is referring about (through reflection) and do some cleanup on that table.

Therefore the logic to cleanup will be the same for every test, but the name of the table will vary according to which test is running. Unfortunately this doesn't work because (seems to me but I am not sure) when I execute any test from MyTest the this.GetType() in DatabaseFixture returns to me the base class which in this case is DatabaseFixture. But I need the children MyTest class to retrieve its properties and find out which table it's referring to! Is this possible to do?





Aucun commentaire:

Enregistrer un commentaire