python - Initializing MEDIA_ROOT before each Django Test -
I want to test my files to create and modify media files. So, like Django tests, a lot happens with the database, before each test, I want to set up a blank MEDIA_ROOT folder.
I thought I would create a temporary folder and point it to MEDIA_ROOT However, I can not understand where the code is placed in which it does, created a special runner
Runner Media sets the root and empties it.
Unfortunately, I tried to create a Usually I would not have been bothered with this, but the cost of forgetting to call parents setup method can be very high - if a person forgets the call and the test is accidentally If you walk on a live system, then there are bad things. EDIT: Temporary Solution I 'I have found that both implement their own runner and a base I am hoping for a more elegant solution. FileSystemTestCase
class which sets the file system in its setup
function, and it receives all my test cases it happens. Although this works, for this, every person needs to write a testcase, who remembers to call my setup
method, because it is not automatically asked. TestCase
were both a temporary MEDIA_ROOT Is set up, so if someone forgets to call my setup
method, the test will be run in the temporary folder of the previous exam, or one installed by runner may fail the test, but Live data will not be wasted
It seems that you are trying to address two different issues:
- Allow tests to run independently (
MEDIA_ROOT
) when testers have done the right thing (i.e. your test class is inherited and yoursetup ()
. - Keeping the facts inaccurate by mistake, the wrong thing is wrong.
In view of this, I think a two- Dimensional view The ion is understandable.Your setup ()
fixes the problem. Setting MEDIA_ROOT
in the test runner, however, hides the fact that your testers do the wrong thing Instead, I focus solely on data protection: for example, you can set MEDIA_ROOT
to none
. MEDIA_ROOT
Will preserve the actual data; it is more likely that the testers will see an error if they Do not use setUp ()
; And reducing code repetition.
A more robust approach will be to write your own test runner, which sets before each test (prepared after the method of handling the jeezo database), but it is for your needs. Can be overkill.
Comments
Post a Comment