djangodjango-testingdjango-tests

StaticLiveServerTestCase Server Error 500


Main urls.py file:

urlpatterns = [

    path(
        'admin/',
        admin.site.urls
    ),

    path(
        '',
        include(
            'employee.urls'
        )
    ),

    path(
        '',
        include(
            'epos.urls'
        )
    ),

    path(
        '',
        include(
            'supplier.urls'
        )
    ),

]

epos.urls:

urlpatterns = [
    path(
        '',
        home_view,
        name='home'
    ),

home_view:

@login_required
def home_view(request):
    # Get all categories
    categories = Category.objects.all()

    # Get the first category which will be selected by default
    selected_category = categories.first()

    # Get the order_items for the selected category (first category)
    products = Product.objects.filter(
        category=selected_category.id
    )

    user = request.user

    tax = Tax.objects.latest('id')

    context = {
        'categories': categories,
        'user': user,
        'title': "EPOS",
        'products': products,
        'selected_category': selected_category,
        'tax': tax
    }

    return render(request, 'epos/epos.html', context)

Test case:

class LoginTests(StaticLiveServerTestCase):

    fixtures = ['fixtures/employee/employee_data.json']

    def setUp(self):

        self.browser = webdriver.Chrome()
        self.browser.get(self.live_server_url)
        self.browser.maximize_window()

    def tearDown(self):
        self.browser.quit()

    def test_login_when_clocked_in(self):
        import ipdb;ipdb.set_trace()
        login_button = self.browser.find_element_by_xpath(
            '//button[normalize-space()="Login"]'
        )

        clock_in_out_button = self.browser.find_element_by_xpath(
            '//button[normalize-space()="Clock In/Out"]'
        )

        pin_input = self.browser.find_element_by_id(
            'pin'
        )

        pin_code = 'some pin'
        employee_name = 'some name'

        pin_input.send_keys(pin_code)
        
        clock_in_out_button.click()

        time.sleep(10)
        login_button.click()

settings.py


ROOT_URLCONF = 'allPOS.urls'
LOGIN_URL = '/login/'

When the test logs in I am redirected to the main webpage which is localhost:46497/ but instead of the page I am getting Server Error 500. This error occurs only when testing. In addition, if I add another path e.g. localhost:46497/analytics it opens the webpage as expected.

Any help would be appreciated.


Solution

  • After 4 hours of debugging, I found out that the issue was related to the database being empty.

    The webpage that I wanted to render is expecting some models to be passed, and since the DB was empty those models weren't passed and hence the crash. In normal circumstances ( ./manage.py runserver) if DEBUG=True it will tell you what is wrong, but for some reason StaticLiveServerTestCase doesn't have this option or at least I am not aware of it. If anyone knows how to enable some debugger for the StaticLiveServerTestCase please feel free to add to this thread.

    So what I did: ./manage.py dumpdata order.tax > data.json - I dumped the data that I needed into a json file and at the beginning of the TestCase I added that data into the fixture

    Hope this helps someone with the same issue!