简体   繁体   English

在我的 django 应用程序中对 javascript 函数进行单元测试

[英]unit test the javascript functions in my django application

I have a Django app, which contains backend logic in views.py and front end logic/user data processing written in javascript and jQuery inside HTML files.我有一个 Django 应用程序,它包含views.py中的后端逻辑和 HTML 文件中用 javascript 和 jQuery 编写的前端逻辑/用户数据处理。 Now I wanted to write unit tests, can I use mocha-chai for doing this or any other alternative to do unit testing a JavaScript code in Django.现在我想编写单元测试,我可以使用 mocha-chai 来做这个或任何其他替代方法来在 Django 中对 JavaScript 代码进行单元测试。

my directory structure我的目录结构

myapp
--->templates
----->base.html
----->home.html

base.html contains: base.html 包含:

<html>
<body>
<h1>WElcome</h1>
<script>
function myfunc(){
console.log("welcome")
//some user logic 
}
</sctipt>

which framework to use in order to do write a unit test case for this 'myfunc' inside the Django template使用哪个框架来为 Django 模板中的“myfunc”编写单元测试用例

We have tried the following:我们尝试了以下方法:

  1. Use the Django testing framework as base使用 Django 测试框架作为基础
  2. extend it to use Nightwatch with a headless browser to run JS扩展它以使用带有无头浏览器的 Nightwatch 来运行 JS
  3. use Selenium for integration tests使用 Selenium 进行集成测试

We failed, however, to get it to work completely because the headless browser did not run all of our JS code.然而,我们没有让它完全工作,因为无头浏览器没有运行我们所有的 JS 代码。 This was some years ago, and the libraries surely have evolved since then.那是几年前的事了,从那时起图书馆肯定已经发展了。

If you are happy with integration tests only:如果您只对集成测试感到满意:

Setup the selenium with Django LiveServerTestCase and a headless browser that runs all of your JS code correctly.使用 Django LiveServerTestCase 和可正确运行所有 JS 代码的无头浏览器设置 selenium。

https://docs.djangoproject.com/en/2.2/topics/testing/tools/#liveservertestcase https://docs.djangoproject.com/en/2.2/topics/testing/tools/#liveservertestcase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中对箭头功能进行单元测试? - How to unit test arrow functions in JavaScript? 无法使用Tape / Sinon对JavaScript函数进行单元测试 - Cannot unit test JavaScript functions with Tape/ Sinon 我应该针对不会出现在我的应用程序中的边缘情况对函数进行单元测试吗? - Should I unit test functions for edge cases that won't occur in my application? 如何在我的 javascript Z2567A5EC93305EB18AC2DZ9 应用程序的数据库层中为 class function 开玩笑地编写单元测试? - How do I write a unit test in jest for a class function in the database layer of my javascript web application? 我应该单独测试我的JavaScript吗? - Should I unit test my JavaScript? 如何对传递给JavaScript函数的匿名回调进行单元测试? - How can I unit test anonymous callbacks passed to JavaScript functions? 将JavaScript单元测试文件放在Web应用程序中的位置 - Where to place JavaScript unit test files in a web application Javascript / AngularJS应用的声纳报告显示零单元测试成功 - sonar report of Javascript/AngularJS application showing zero unit test success 为什么我的Javascript代码无法在Chai中通过单元测试用例 - why doesnt my Javascript code pass unit test case in chai 在我的 Javascript 单元测试中,expect(true).toBeTruthy() 失败 - In my Javascript Unit test, expect(true).toBeTruthy() fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM