Issue: Diferent results when unit testing one unit test file and unit testing a folder with several files with unit tests
Author: fmors
State: closed
Created: 2018-10-19 15:48:19

Here is the context... I have implemented a simple test for a project I'm working on, where this test instantiates a SQLite database on memory, creates a table based on a model and adds some rows to that table, later asserting that my Tornado application GET the rows correctly. The peewee version I'm using is 3.7.0, but with 3.7.1 the behavior is the same. Then I have 2 scenarios:

1. running the functional test with `py.test tests/my_test.py` works perfectly. It injects the rows into the table and, when my Tornado application GET the rows, it will return the rows and the assertion will work.

1. running the functional test with discovery `py.test` does not work. The assertion shows that no row was added to the table, though no error is shown in the process.

This is the model and the test:

```
class BaseModel(Model):
    _id = AutoField()

    class Meta:
        database = Proxy()

    @staticmethod
    def initialize_db(db):
        BaseModel._meta.database.initialize(db)


class Element(BaseModel):
    name = CharField()


def initialize_db_and_model():
    db = SqliteDatabase(":memory:")

    model.BaseModel.initialize_db(db)  # model is a package where Element and BaseModel are definedI can give you more details about this issue, but I can't share the entire source-code due to privacy issues.

    db.connect()
    db.create_tables([model.Element])

    return db, model


class TestExperimentHandler(AsyncHTTPTestCase):
I can give you more details about this issue, but I can't share the entire source-code due to privacy issues.
    def setUp(self):
        self.db, self.model = initialize_db_and_model()

        # Populate some data
        self.model.Element.create(name='name1')

    def get_app(self):
        return tornado_application(self.db, self.model)

    def test_get_all(self):
        response = self.fetch('/element')  # return a list of elements from table "element"
        elements = json.loads(response.body.decode('utf-8'))
        elements_names = list(map(lambda x: x['name'], experiments['_items']))
        self.assertEqual(experiments_names, ['name1'])
```

What I could notice, by debugging this application, is that the `Model.create()` method enters the `save()` method when executing the test file individually, but when executing this test file with other tests in a test discovery process, `Model.create()` passes through `save()` without entering there:

```
    @classmethod
    def create(cls, **query):
        inst = cls(**query)
        inst.save(force_insert=True)
        return inst
```

I have printed the value of `inst` before returning (on `create()`) and added some prints inside `save()`. For the case of starting unit test specifying the file, `inst` equals to 1 before returning, and it enters `save()` method. However, for the case of unit test with discovery, `inst` equals to `None` before returning, and it does not enter `save()` method.

Please let me know if you need any other information.

Have you tried with a file-based database? That might provide a clue.

No idea but it's certainly not a peewee issue.